【Gym 102202】 D - 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
Input
2 3
Output
4
B+=B
B+=A
A+=A
A+=A

題意:通過上述四種操作,可以多少步使得A==B並輸出路徑

思路:

訓練的時候用暴力程序套結論,發現最後相同時的A,B一定是2的冪次,那麼只需要將兩個數湊成10000…000的形式即可。即是消去低位的1的過程,可以讓兩個數的lowbit變成同一位然後相加,消去lowbit,再以此類推。代碼化的時候,可以直接在尾部添至相同個數的0,如110100和1001000,可以前者添加3個0後者添加兩個0,使得雙方變成110100000 100100000,這樣各自的lowbit就都在低6位。這時候兩數相加就消掉了一個1,往前進位,直至只剩一個1 。 過程發現我們不需要保留後面的0,所以直接去掉各自的0來比較即可。這裏就相當於一個逆操作。消掉各自的0的前提就是湊到個數相同的0,而這個時候就相當於讓對方加上自己0的個數。詳見代碼,每次處理完還要判斷一次大小關係,注意是給大的左移,因爲這樣又可以消去0了。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 3e4+2;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a,b;
vector<ll> ans;
void solve()
{
    while(a!=b)
    {

         ll cnt1 = 0,cnt2=0;
         while(a%2==0&&a)
         {
             a >>= 1;
             cnt1++;
         }
        while(b%2==0&&b)
         {
             b >>= 1;
             cnt2++;
         }
         rep(i,1,cnt1) ans.pb(2);
         rep(i,1,cnt2) ans.pb(1); if(a==b)break;
         if(a<b)
         {
             b = b + a;
             ans.pb(4);
           while(b&1==0&&b)
            {
                b >>= 1;
                ans.pb(1);
            }
         }
         if(b<a)
         {
             a = a + b;
             ans.pb(3);
           while(a&1==0&&a)
            {
                a >>= 1;
                ans.pb(2);
            }
         }
    }
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++)
    {
        if(ans[i]==1)
        {
            printf("A+=A\n");
        }
        else if(ans[i]==2)
        printf("B+=B\n");
        else if(ans[i]==3)
        printf("A+=B\n");
        else
        printf("B+=A\n");
    }
}


int main()
{
    cin>>a>>b;
    solve();
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章