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[];
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=; tA='A';
tB='B'; if(x<y)
{
swap(x,y);
swap(tA,tB);
} if(gcd(x,y)!=)
puts("Impossible");
else
{
for(int i=;i<cou-;++i,swap(tA,tB))
cout<<ans[i]<<tA;
cout<<ans[cou-]-<<tA<<endl;
} return ;
}

(中等) CF 585C Alice, Bob, Oranges and Apples,矩阵+辗转相除。的更多相关文章

  1. Alice, Bob, Oranges and Apples CodeForces - 586E

    E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...

  2. CF6C Alice, Bob and Chocolate

    CF6C Alice, Bob and Chocolate 题目链接 写了一天搜索写的有点累了,就顺手水了一道CF的模拟题 这道题就是简单的模拟整个题的过程,注意最后输出的形式就好了QWQ AC代码如 ...

  3. Alice and Bob 要用到辗转相减

    Alice and BobTime Limit: 1 Sec  Memory Limit: 64 MBSubmit: 255  Solved: 43 Description Alice is a be ...

  4. Codeforces Beta Round #6 (Div. 2 Only) C. Alice, Bob and Chocolate 水题

    C. Alice, Bob and Chocolate 题目连接: http://codeforces.com/contest/6/problem/C Description Alice and Bo ...

  5. Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和

    B. Alice, Bob, Two Teams 题目连接: http://www.codeforces.com/contest/632/problem/B Description Alice and ...

  6. codeforces 632B B. Alice, Bob, Two Teams(暴力)

    B. Alice, Bob, Two Teams time limit per test 1.5 seconds memory limit per test 256 megabytes input s ...

  7. cf C. Alice and Bob

    http://codeforces.com/contest/347/problem/C 这道题就是求出n个数的最大公约数,求出n个数的最大值,总共有max1/gcd-n个回合.然后判断如果回合数%2= ...

  8. C - Alice, Bob and Chocolate(贪心)

    Problem description Alice and Bob like games. And now they are ready to start a new game. They have ...

  9. 计蒜客 ACM训练联盟周赛 第一场 Alice和Bob的Nim游戏 矩阵快速幂

    题目描述 众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手. Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个), ...

随机推荐

  1. C#中的Virtual、Override和new关键词理解

    来源:http://blog.csdn.net/jackiezhw/article/details/2673992 在 C# 中,派生类可以包含与基类方法同名的方法. 基类方法必须定义为 virtua ...

  2. MD5 32位加密算法源码(测试通过)(系转载 飞扬天下)

    供自己学习使用 md5.h文件 #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Type ...

  3. zk reconnect

    http://blog.csdn.net/hengyunabc/article/details/41450003 http://blog.csdn.net/hengyunabc/article/det ...

  4. TP4056大电流1A使用注意事项

    源:TP4056大电流1A使用注意事项 TP4056为南京拓微集成电路有限公司推出的锂电池充电产品系列中的大电流充电产品.具有最大电流1A,峰值电流1.1A,良好环境下甚至峰值1.2A的单节锂离子电池 ...

  5. MySQL 异常错误码使用 及 对照表

    DECLARE CONTINUE HANDLER FOR SQLWARNING, SQLEXCEPTION, NOT FOUND BEGIN GET DIAGNOSTICS CONDITION cod ...

  6. 【jsp/servlet】 javaweb中的一些简单问题整理

    1 jsp工作原理 答: 动态网页技术标准blabla...jsp程序的工作方式为请求/响应模式,客户端发出http请求,jsp程序收到请求后进行处理,并返回处理的结果. jsp程序需要运行在特定的w ...

  7. 【最长下降子序列的长度和个数】 poj 1952

    转自http://blog.csdn.net/zhang360896270/article/details/6701589 这题要求最长下降子序列的长度和个数,我们可以增加数组maxlen[size] ...

  8. php多进程实现

    php多进程实现 PHP有一组进程控制函数(编译时需要–enable-pcntl与posix扩展),使得php能在nginx系统中实现跟c一样的创建子进程.使用exec函数执行程序.处理信号等功能. ...

  9. js屏蔽浏览器右键菜单

    <script type="text/javascript"> function doNothing(){ window.event.returnValue=false ...

  10. c++模板两个数的加法

    1.最简单的情况: template<class T> T Add(const T& a, const T& b) { return a + b; } 缺点是不能够处理不同 ...