Game|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 2 #include 3 #include 4 #include 5 #include 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){ 14return (b>0)?GDC(b,a%b):a; 15 } 16 17 int main(){ 18while(cin>>t&&t){ 19int ans=1; 20while(t--){ 21cin>>x>>n>>y>>m; 22a=0; 23b=x; 24for(int i=1; i<=n; i++){ 25if(b>a){ 26a+=b; 27} 28else{ 29b+=a; 30} 31} 32 33if(a>=b){ 34a+=y; 35} 36else{ 37b+=y; 38} 39 40 41ll sum; 42if(a>b){ 43sum=GDC(a,b); 44} 45else{ 46sum=GDC(b,a); 47} 48printf("Case %d: %I64d\n",ans,sum); 49ans++; 50} 51} 52return 0; 53 }


【Game|Game of Peace】转载于:https://www.cnblogs.com/Kiven5197/p/5700126.html

    推荐阅读