zoukankan      html  css  js  c++  java
  • A Plus Equals B

    Problem:

    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

        #include <iostream>
        #include <vector>
        using namespace std;
        #define int long long
        int a , b;
        vector < int > s;
        signed main ( void )
        {
            cin >> a >> b;
            while ( a != b ) {
                while ( a % 2 == 0 ) {
                    s.push_back ( 1 );
                    a /= 2;
                }
                while ( b % 2 == 0 ) {
                    s.push_back ( 2 );
                    b /= 2;
                }
                if ( a == b ) break;
                if ( a < b ) {
                    b += a;
                    s.push_back ( 3 );
                } else {
                    a += b;
                    s.push_back ( 4 );
                }
            }
            cout << s.size ( ) << endl;
            for ( int i = 0 ; i < s.size ( ) ; i++ ) {
                if ( s [ i ] == 1 ) {
                    cout << "B+=B" << endl;
                } else if ( s [ i ] == 2 ) {
                    cout << "A+=A" << endl;
                } else if ( s [ i ] == 3 ) {
                    cout << "B+=A" << endl;
                } else {
                    cout << "A+=B" << endl;
                }
            }
            return 0;
        }
    

      

  • 相关阅读:
    事务传播机制,搞懂。
    洛谷 P1553 数字反转(升级版) 题解
    洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here 题解
    洛谷 P1055 ISBN号码 题解
    洛谷 P2141 珠心算测验 题解
    洛谷 P1047 校门外的树 题解
    洛谷 P1980 计数问题 题解
    洛谷 P1008 三连击 题解
    HDU 1013 题解
    HDU 1012 题解
  • 原文地址:https://www.cnblogs.com/jaszzz/p/12715961.html
Copyright © 2011-2022 走看看