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

构造矩阵\ 
那么A操作相当于对M左乘矩阵这里写图片描述,B操作相当于对M左乘矩阵这里写图片描述 
根据矩阵的性质我们知道这里写图片描述这里写图片描述 
所以我们有这里写图片描述这里写图片描述 
最后的结果是a+b=x,c+d=y,那么我们反向来看这个操作序列的话,最后几步操作如果是A操作,那么相当于这里写图片描述 
轻易看出这是辗转相除法的第一步,即将(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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值