天天看點

Codeforces Round #325 (Div. 2) E 數論

連結:戳這裡

E. Alice, Bob, Oranges and Apples time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over.

You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with.

Input

The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 1018, xy > 1) — the number of oranges and apples that were initially in the bag.

Output

Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 106 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 106 characters. If there are several possible answers, you are allowed to print any of them.

If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible.

Examples

input

1 4

output

3B

input

2 2

output

Impossible

input

3 2

output

1A1B

Note

In the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples.

In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges.

In the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples.

題意:

甲乙在路上看到兩袋子水果,一袋子是蘋果,一袋子是橘子。

然後甲手上拿一個蘋果0個橘子,乙手上拿0個蘋果,1個橘子。

下面是兩個操作A和B

操作A表示将甲手上的所有水果都給乙,然後自己再去袋子裡面拿一份和原來相同數量的兩種水果

操作B表示将乙手上的所有水果都給甲,然後自己再去袋子裡面拿一份和原來相同數量的兩種水果

要求你輸出一串指包含AB的串,表示操作的序列。滿足最後拿完所有袋子裡的水果之後

蘋果總數為X橘子總數為Y,給你這樣的X和Y,問是否有這樣的序列

思路:

參照題解:http://www.ithao123.cn/content-10513754.html

構造矩陣

Codeforces Round #325 (Div. 2) E 數論

那麼A操作相當于對M左乘矩陣

Codeforces Round #325 (Div. 2) E 數論

,B操作相當于對M左乘矩陣

Codeforces Round #325 (Div. 2) E 數論

根據矩陣的性質我們知道

Codeforces Round #325 (Div. 2) E 數論

Codeforces Round #325 (Div. 2) E 數論

是以我們有

Codeforces Round #325 (Div. 2) E 數論

Codeforces Round #325 (Div. 2) E 數論

最後的結果是a+b=x,c+d=y,那麼我們反向來看這個操作序列的話,最後幾步操作如果是A操作,那麼相當于

Codeforces Round #325 (Div. 2) E 數論

輕易看出這是輾轉相除法的第一步,即将(x,y)變成(x%y,y),是以此題合法的操作序列其實就是輾轉相除的反向操作,由于初始狀态是(1,1),故不合法情況是gcd(x,y)!=1,而合法時每次的操作數為x/y(或者y/x),x>y時是A操作,y>x時是B操作 

代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll x,y;
ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}
int main(){
    scanf("%I64d%I64d",&x,&y);
    if(gcd(x,y)!=1LL) printf("Impossible\n");
    else {
        while(x && y){
            if(x>y){
                if(y==1) x--;
                printf("%I64dA",x/y);
                x=x%y;
            } else {
                if(x==1) y--;
                printf("%I64dB",y/x);
                y=y%x;
            }
        }
    }
    return 0;
}
           

繼續閱讀