(中等) 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个), ...
随机推荐
- MySQL数据库MyISAM和InnoDB存储引擎的比较【转载】
转自 http://www.cnblogs.com/panfeng412/archive/2011/08/16/2140364.html MySQL有多种存储引擎,MyISAM和InnoDB是其中常用 ...
- 快速玩转Apple Pay开发
快速玩转Apple Pay开发 更新时间:2016年02月20日09时25分 来源:传智播客 Apple Pay 2016年2月18日上午,苹果公司宣布,与中国银联达成合作,正式在中国大陆上线Appl ...
- ubuntu 上下载PHP的源代码
参考: https://vpsineu.com/blog/how-to-build-and-install-php-5-6-9-from-source-on-ubuntu-14-04-vps/ 直接 ...
- zTree异步加载并初始化树时全部展开(贴主要代码)
<%@page pageEncoding="UTF-8"%> <%@include file="/commons/include/html_doctyp ...
- Zookeeper: configuring on centos7
thispassage is referenced, appreciated. ZooKeeper installation: Download from this site Install java ...
- JSP中文编码问题
这个乱码问题是最简单的乱码问题.一般新会出现.就是页面编码不一致导致的乱码. <%@ page language="java" pageEncoding="UTF- ...
- 空间表SpaceList
比如在建一个成绩管理系统,这时候定义的名字一般都是char szName[20],这样比较浪费,其实不只是定义名字,定义好多变量都这样,并没有体现动态. 此处出现空间表(SpaceList),通过指针 ...
- 转:Windows下用sftp自动下载文件
远程服务器是Linux操作系统,没有ftp服务,可以ssh,数据库每天2:00会自动创建一个备份文件,本地计算机是windows操作系统,希望用sftp每天3:00下载远程服务器上的备份文件.本地系统 ...
- 让shell 变得容易理解
1.重建你的语义模型(简单语义模型)2.变量,参数和方法命名3.测试用例4.足够的组块
- Android ---paint类
引自:http://www.cnblogs.com/-OYK/archive/2011/10/25/2223624.html Android Paint和Color类 要绘图,首先得调整画笔,待画 ...