(中等) CF 585C Alice, Bob, Oranges and Apples,矩阵+辗转相除。
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.
真是日了哈士奇的一场,掉了70多分。。。
这题挺好的,可惜比赛的时候时间不够没能把公式推完。。。
题意就是两个人玩游戏,然后你给我我给你的,最后完成之后正好那些个橙子和苹果。
先分开想橙子和苹果,假设现在Alice有a个橙子,Bob有b个,搞一个2行1列的矩阵来存,然后每次A操作就是
B操作就是 。
然后k次A操作就是。
B操作同理。
然后初始时橙子是,苹果是。
假设A和B的操作序列最后乘起来之后的矩阵是。
那么最后橙子的结果是,苹果是。
所以a+c=x,b+d=y。
然后当时到了这里就卡了一下了。。。
这时看乘上一个A操作,也就是在已经有的操作序列最前面加上一个A操作, ,得到的结果为,然后 b+d 没变,a+c 变成了 a+c+k×(b+d),就像时 x,y变成了 x+ky,y。
也就是说在操作序列开头加一个A的话就变成了x+ky,y。
和辗转相除十分像,题目就变成了 把 1,1通过操作变成 x,y,就是辗转相除的逆操作而已。
代码如下:
// ━━━━━━神兽出没━━━━━━ // ┏┓ ┏┓ // ┏┛┻━━━━━━━┛┻┓ // ┃ ┃ // ┃ ━ ┃ // ████━████ ┃ // ┃ ┃ // ┃ ┻ ┃ // ┃ ┃ // ┗━┓ ┏━┛ // ┃ ┃ // ┃ ┃ // ┃ ┗━━━┓ // ┃ ┣┓ // ┃ ┏┛ // ┗┓┓┏━━━━━┳┓┏┛ // ┃┫┫ ┃┫┫ // ┗┻┛ ┗┻┛ // // ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy // Created Time : 2015年10月12日 星期一 18时56分45秒 // File Name : E.cpp #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; long long x,y; char tA,tB; long long ans[100000]; int cou; long long gcd(long long x,long long y) { if(!y) return x; ans[cou++]=x/y; return gcd(y,x%y); } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); cin>>x>>y; cou=0; tA='A'; tB='B'; if(x<y) { swap(x,y); swap(tA,tB); } if(gcd(x,y)!=1) puts("Impossible"); else { for(int i=0;i<cou-1;++i,swap(tA,tB)) cout<<ans[i]<<tA; cout<<ans[cou-1]-1<<tA<<endl; } return 0; }