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. mynotebook

    www.linux.org/threads/beats-audio-on-linux.4443/ askubuntu.com/questions/303775/envy-15-beats-audio- ...

  2. MySQL出现Errcode:28错误提示解决办法

    mysql出现Error writing file \'xxx\'( Errcode:28)的原因有很多种,下面我来总结一些常用的关于引起Errcode:28错误原因与解决方法.   问题一,是log ...

  3. Java学习笔记之static

    1.static可以用于修饰成员变量.方法以及块,并不会改变类中成员的权限修饰,如:private修饰的成员变量,类外只能类名或非私有方法调用,而不能使用对象名调用. 2.static方法和成员变量, ...

  4. ExceL转PDF

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Excel = ...

  5. UVA106 - Fermat vs. Pythagoras

    假设x为奇数,y为偶数,则z为奇数,2z与2x的最大公因数为2,2z和2x可分别写作 2z = (z + x) + (z - x) 2x = (z + x) - (z - x) 那么跟据最大公因数性质 ...

  6. iOS字符串转化成CGFloat

    NSString *str = @"abc"; [str floatValue];

  7. 天天果园,中粮我买网等生鲜APP竞品分析

    奈何对生鲜行业的品类,价格,供应链不熟悉,想先从APP开始来了解生鲜行业和各个生鲜企业,若有不足之处,还望海量,也请帮忙指正. 选取了以下竞品:天天果园,易果生鲜,一米鲜,中粮我买网,爱鲜蜂,每日优鲜 ...

  8. Java BigDecimal Class

    Using BigDecimal to perform precise calculations with floats. BigDecimal is a class type. So declare ...

  9. Android 开发——如何显示 GIF 动画

    gif 图动画在 android 中还是比较常用的,比如像新浪微博中,有很多 gif 图片,而且展示非常好,所以我也想弄一个.经过我多方的搜索资料和整理,终于弄出来了,其实 github 上有很多开源 ...

  10. display:block;inline;inline-block大总结

    总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).block元素通 ...