(中等) 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[];
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,矩阵+辗转相除。的更多相关文章
- Alice, Bob, Oranges and Apples CodeForces - 586E
E - Alice, Bob, Oranges and Apples CodeForces - 586E 自己想的时候模拟了一下各个结果 感觉是不是会跟橘子苹果之间的比例有什么关系 搜题解的时候发现了 ...
- CF6C Alice, Bob and Chocolate
CF6C Alice, Bob and Chocolate 题目链接 写了一天搜索写的有点累了,就顺手水了一道CF的模拟题 这道题就是简单的模拟整个题的过程,注意最后输出的形式就好了QWQ AC代码如 ...
- Alice and Bob 要用到辗转相减
Alice and BobTime Limit: 1 Sec Memory Limit: 64 MBSubmit: 255 Solved: 43 Description Alice is a be ...
- 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 ...
- 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 ...
- 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 ...
- cf C. Alice and Bob
http://codeforces.com/contest/347/problem/C 这道题就是求出n个数的最大公约数,求出n个数的最大值,总共有max1/gcd-n个回合.然后判断如果回合数%2= ...
- C - Alice, Bob and Chocolate(贪心)
Problem description Alice and Bob like games. And now they are ready to start a new game. They have ...
- 计蒜客 ACM训练联盟周赛 第一场 Alice和Bob的Nim游戏 矩阵快速幂
题目描述 众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手. Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个), ...
随机推荐
- Oracle record 解锁
Oracle修改表中记录时出现record is locked by another user的弹出框问题 是因为在操作表中数据时没有commit,导致表被锁. 执行下面两行语句: (1)查看被锁表的 ...
- docker !veth
https://github.com/docker/docker/issues/11889
- overlay
http://dockone.io/article/237 http://blog.cloud66.com/docker-with-overlayfs-first-impression/ http:/ ...
- table详解
1.tr 元素定义表格行,th 元素定义表头,td 元素定义表格单元. tr内是th还是td可由自己定义,th,td可存在于任一行,th与td的区别在与th字体更粗 2.定义一个table默认有bor ...
- MSG 结构
MSG 消息结构 在 Windows 程序中,消息是由 MSG 结构体来表示的. 结构原型: typedef struct tagMSG { HWND hwnd; UINT message; ...
- 函数求值一<找规律>
函数求值 题意: 定义函数g(n)为n最大的奇数因子.求f(n)=g(1)+g(2)+g(3)+-+g(n).1<=n<=10^8; 思路: 首先明白暴力没法过.问题是如何求解,二分.知道 ...
- 文本格式ANSI,Unicode等有什么区别
首先DBCS是亚洲的字符集,包含了ANSI,ANSI也就是ASCII值为0-255之间的字符,当字符为ANSI时,存放于文件中占用的是一个字节.如果是非ANSI的呢,则占用两字节.用VB的ASC函数可 ...
- TriggerPrefab 拖拽物体
模拟经营类游戏 有一个特点,就是 拖拽物体.常见的有<帝国><红警><部落战争><凯撒大帝>等等 2d 拖拽 大部分都是 用 OnDrag 方法来 拖动 ...
- HDU - 1702 ACboy needs your help again!(栈和队列)
Description ACboy was kidnapped!! he miss his mother very much and is very scare now.You can't image ...
- 转:JMeter--使用代理录制Web性.能测试脚.本
Apache JMeter是一款纯Java的应用程序,用于对软件或系统做性.能测试,如压力测试.负载测试.最初设计是用于web应用测试,由于开源其测试组件不断被扩充,逐步扩展到其他测试领域中. 接下给 ...