题意:
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
题解:
虽然这题的数字是在向上叠加的,但我们不能真的去向上叠加,一是因为向上坑太大,没办法处理两个数的关系,二是对于大数会爆long long。观察后可以发现A+=A的本质是B-=B,B+=B的本质是A-=A,这样处理可以保持两个数时刻处于纠缠的状态。因此我们只需遇到偶数就除以二并时刻进行辗转相减即可。
代码:
#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");
}
}
}