(中等) 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个), ...
随机推荐
- psy
本文的重点是讲解如何运用心理线指标看盘,运用周线月线的心理线来抓住大盘的顶部和底部的研究.分析研究的材料都来源于沪市历史上的顶部和底部的历史数据.从psy数据所得出的结论大多数是有效的,只有个别时期的 ...
- web开发在线调试
来源: http://www.cnblogs.com/itech/archive/2012/09/23/2698754.html 通常我们开发web时候,使用ie的developertoolgs,或c ...
- rebar
www.cnblogs.com/panfeng412/archive/2011/08/14/2137990.html
- Phonegap解决错误:Error initializing Cordova:Class not found
Phonegap 解决错误: Alert [ERROR]Error initializing Cordova:Class not found 发现bug后找原因 网上说是 因为找不到 ...
- C# 将字符串转为ऩ这种的 html实体编码
1.字符串转为html实体编码 private string GetHtmlEntities(string str) { string r = string.Empty; ; i < str.L ...
- java中对List中对象排序实现
package com.test; import java.util.ArrayList; import java.util.Collections; import java.util.Compara ...
- Form表单的post 和get跳转区别
post是隐示请求 ----- 安全 get显示请求不安全,会在URL上显示路径和参数
- OpenGL—Android 开机动画源码分析二
引自http://blog.csdn.net/luoshengyang/article/details/7691321/ BootAnimation类的成员函数的实现比较长,我们分段来阅读: 第三个开 ...
- 转一篇分析C语言调用时栈的变化的好文
http://blog.csdn.net/zsy2020314/article/details/9429707
- Loadrunner脚本录制注意事项(七)
1.手动走一遍被测业务,达到熟悉理解业务,注意是否和服务器有数据交互,为脚本是否需要关联做准备: 2.浏览器选择IE8/9较好,选择其他浏览器可能会有各种问题.(a.IE设置:内容-设置-去掉所有选项 ...