Codeforces 102202D-A Plus Equals B【思维】 难度:**
題意:
A?+?B is a problem used to test one’s basic knowledge for competitive programming. Here is yet another boring variation of it.
You have two integers, A and B. You want to make them equal. To do so, you can perform several steps, where each step is one of the following:
A+=A
A+=B
B+=A
B+=B
Unfortunately, A?+?B is a hard problem for us, so you are allowed to make at most 5000 steps.
Input
Two integers A,?B are given. (1?≤?A,?B?≤?1018).
Output
In the first line, print a single integer n (0?≤?n?≤?5000) denoting the number of steps.
In next n lines, print one of the following strings to denote your desired operation: “A+=A”, “A+=B”, “B+=A”, or “B+=B”.
Any sequence of steps that yields the desired result will be judged correct.
Example
inputCopy
2 3
outputCopy
4
B+=B
B+=A
A+=A
A+=A
題解:
雖然這題的數(shù)字是在向上疊加的,但我們不能真的去向上疊加,一是因?yàn)橄蛏峡犹?#xff0c;沒辦法處理兩個(gè)數(shù)的關(guān)系,二是對(duì)于大數(shù)會(huì)爆long long。觀察后可以發(fā)現(xiàn)A+=A的本質(zhì)是B-=B,B+=B的本質(zhì)是A-=A,這樣處理可以保持兩個(gè)數(shù)時(shí)刻處于糾纏的狀態(tài)。因此我們只需遇到偶數(shù)就除以二并時(shí)刻進(jìn)行輾轉(zhuǎn)相減即可。
代碼:
#include<stdio.h> int main() {long long a,b,c,d;int times=0;scanf("%lld%lld",&a,&b);c=a;d=b;while(a!=b){while(a%2==0){times++;a/=2;}while(b%2==0){times++;b/=2;}if(a==b)break;if(a<b)b+=a;else a+=b;times++;}printf("%d\n",times);a=c;b=d;while(a!=b){while(a%2==0){printf("B+=B\n");a/=2;}while(b%2==0){printf("A+=A\n");b/=2;}if(a==b)break;if(a<b){b+=a;printf("B+=A\n");}else{a+=b;printf("A+=B\n");}} }總結(jié)
以上是生活随笔為你收集整理的Codeforces 102202D-A Plus Equals B【思维】 难度:**的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企业业财一体化落地难?ERP系统推进业财
- 下一篇: 显示器的bit是什么意思,8bit和10