Game of Peace

Time Limit: 4000ms, Special Time Limit:10000ms, Memory Limit:65536KB
Total submit users: 20, Accepted users: 12
Problem 13553 : No special judgement
Problem description

Bob has learned a new magic trick that needs a very special preparation. Once he masters the trick he will be able to bring peace to the world, but if he fails, the world will be destroyed.

The preparation is performed as follows: There are two containers, initially one is empty and the other one has X marbles. Bob has a Marble Cloning Machine, it clones the marbles in the container with the larger number of marbles, then pours the new clones into the other container (e.g. if the two containers have 7 and 4 marbles, after the cloning step they will have 7 and 11 marbles). The machine does this cloning operation exactly M times. However, there is a bug in the machine, after it performs N cloning operations (N ≤ M ), it will add Y extra marbles to the container with the larger number of marbles. Then the machine will continue normally with the cloning operation exactly M - N times.

During the cloning operations, if both containers have the same number of marbles, any of them can be considered the one with the larger number of marbles.

Now, the bug in Bob’s machine is threatening to destroy the world. But his nerdy friend Alice told him that she knows how to fix it. All he has to do is to calculate the greatest common divisor of the sizes of the two containers after the cloning machine is done. Can you help Bob save the world?

Input

Your program will be tested on one or more test cases. The first line of the input will be a single integer T (1 ≤ T ≤ 1,000) representing the number of test cases. Followed by T test cases. Each test case will consist of a single line, containing 4 integers separated by a single space X , N , Y and M (1 ≤ X , Y ≤ 1,000) (0 ≤ N ≤ 70) (N ≤ M ≤ 100,000) which are the numbers as described above.

Output

For each test case print a single line containing “Case n:” (without quotes) where n is the test case number (starting from 1) followed by a space then the greatest common divisor of the sizes of the two containers after the machine is done.

Sample Input
2
4 3 6 5
5 1 15 2
Sample Output
Case 1: 2
Case 2: 5
Judge Tips
Note In the first sample test case, the number of marbles in each container will be the following after each step: (4, 0), (4, 4), (4, 8), (12, 8), (18, 8), (18, 26), (44, 26). The greatest common divisor of 44 and 26 is 2.

题意:

有两个容器,一个为空,另一个有x大理石,总共有m次操作,每次操作可以从两容器较大的一个中克隆所有大理石并加到另一个容器中,执行完第n次操作后要在较大的容器中额外加入y大理石,再继续剩余m-n操作。求最终两容器中个数的最大公约数。

附AC代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstdio>
 6 using namespace std;
 7
 8 typedef __int64 ll;
 9
10 ll t,x,n,y,m;
11 ll a,b;
12
13 ll GDC(ll a,ll b){
14     return (b>0)?GDC(b,a%b):a;
15 }
16
17 int main(){
18     while(cin>>t&&t){
19         int ans=1;
20         while(t--){
21             cin>>x>>n>>y>>m;
22             a=0;
23             b=x;
24             for(int i=1;i<=n;i++){
25                 if(b>a){
26                     a+=b;
27                 }
28                 else{
29                     b+=a;
30                 }
31             }
32
33             if(a>=b){
34                 a+=y;
35             }
36             else{
37                 b+=y;
38             }
39
40
41             ll sum;
42             if(a>b){
43                 sum=GDC(a,b);
44             }
45             else{
46                 sum=GDC(b,a);
47             }
48             printf("Case %d: %I64d\n",ans,sum);
49             ans++;
50         }
51     }
52     return 0;
53 }
时间: 2024-08-06 10:11:39

Game of Peace的相关文章

HDU 5434 Peace small elephant 状压dp+矩阵快速幂

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: 108 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 小明很喜欢国际象棋,尤其喜欢国际象棋里面的大象(只要无阻挡能够斜着走任意格),但是他觉得国际象棋里的大象太凶残了,于是他

几个linux终端的有趣玩法

1.sl 还可以给别人搞恶作剧: alias ls=sl 这样别人使用 ls 列出目录和文件的时候,却出现一个小火车,那滋味一定很酸爽哈哈哈哈! 2. fortune 这个命令会随机输出有趣的话,比如名言或笑话.同样需要先安装. apt-get install fortune (for aptitude based system) yum install fortune (for yum based system) [email protected]:~$ fortune There is no

电影功夫熊猫使用的单词分析

你英语四级过了吗?功夫熊猫看了吗?功夫熊猫使用了995个英语单词,你会说很简单吧,别急,我给你分析一下,这些单词中有236个单词不在四级词汇范围内,花两分钟时间看看你是否认识这些单词,单词后面跟的数字表示该单词在电影中出现的次数. 你也可以获取本文的分析程序,这样你就可以分析其他电影了.看一部电影之前,先通过这种方式分析一下,然后学习自己不认识的单词,然后再去看电影,如此这样坚持下去,英语水平就会有很大的提升. words(995): 1. you 2492. the 1893. i 1844.

根据76大细分词性对单词进行归组(二)

词性的重要性不言而喻,尤其是对于自然语言处理来说,哪怕就是记单词,根据词性对单词进行归组也是非常有帮助的. superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 各大词性及其包括的词: 32.N-COUNT-COLL(可数集合名词) (词数:50) 1 aristocracy army array audience band 2 cast chapter command commission committee 3 co

词组习语3057组

superword是一个Java实现的英文单词分析软件,主要研究英语单词音近形似转化规律.前缀后缀规律.词之间的相似性规律等等. 1 Anointing of the Sick British English 2 Civvy Street Clerk of the Closet 3 I mean I must say 4 I suppose so I will thank you to do something 5 Incoming mail server Lithium battery 6 M

【Tim Sweeney】Why C++ for Unreal 4?

The first three generations of the Unreal Engine included a sandboxed scripting language, UnrealScript, which provided a simple interface for gameplay programming that was shielded from the complexity of the C++ engine.The scripting approach is very

大端小端是什么?如何区分?

    今天我们谈谈计算机中的大端小端以及网络中的大端小端.参考了网上好多资料以后,从众多资料中我也按照自己的理解方式,总结一下. 学习一个新东西:我们依然是先了解一下大端小端的历史: 讲故事 这个就需要求救一下度娘,以下故事来自度娘,真假我也不知道,权当听故事: "大端"和"小端"可以追溯到1726年的Jonathan Swift的<格列佛游记>,其中一篇讲到有两个国家因为吃鸡蛋究竟是先打破较大的一端还是先打破较小的一端而争执不休,甚至爆发了战争.19

Java集合类详解(转)

Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).JavaSDK不提供直接继承自Collection的类,javaSDK提供的类都是继承自Collection的"子接口"如List和Set. 所

ubuntu15.04中安装mysql和mysql-workbench

本文主要解说mysql在ubuntu中的安装和使用.以及mysql workbench的安装和使用.mysql是一个开源的数据库软件.甲骨文的一个产品,是一个性能较好的数据库软件.mysql workbench是一款专为MySQL设计的ER/数据库建模工具. 1.mysql的安装 这里介绍最简单的方式通过apt-get安装. 安装命令: sudo apt-get update sudo apt-get install mysql-server mysql-client 安装过程仅仅须要设置roo