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 }

 

posted @ 2016-07-24 09:34  Kiven#5197  阅读(552)  评论(0编辑  收藏  举报