Game of Peace

Source: Internet
Author: User
Tags greatest common divisor

Time limit: 4000ms, special time limit:10000ms, Memory Limit:65536KB
Total Submit users: 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'll be a able to bring peace to the world, but if he fails, the world would be destroyed.

The preparation is performed as Follows:there is a 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 and then pours th e new clones into the other container (e.g. if the containers has 7 and 4 marbles, after the cloning step they would h Ave 7 and 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'll add Y extra marbles to th e container with the larger number of marbles. Then the machine would continue normally with the cloning operation exactly m-n times.

During The cloning operations, if both containers has the same number of marbles, any of them can be considered the one W ith the larger number of marbles.

Now, the bug in Bob's machine was threatening to destroy the world. But he nerdy friend Alice told him that she knows how to fix it. All he have to do are to calculate the greatest common divisor of the sizes of the and the containers after the is done. Can Help Bob Save the world?



Input

Your program would be tested on one or more test cases. The first line of the input is a single integer T (1≤t≤1,000) representing the number of test cases. Followed by T test cases. Each test case would consist of a A, 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 is 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 fro M 1) followed by a space then the greatest common divisor of the sizes of the and the the the-the-the-machine was done.



Sample Input
24 3 6 55 1 15 2
Sample Output
Case 1:2case 2:5
Judge Tips
Note The first sample test case, the number of marbles in each container would 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 2.

Test instructions

There are two containers, one is empty, the other has X marble, a total of M operations, each operation can be cloned from the larger one of the two containers of all the marble and added to another container, after the nth operation to add additional Y marble in the larger container, and then continue the remaining m-n operation. Finally, the greatest common divisor of the number of two containers is obtained.

The AC code is attached:

1#include <iostream>2#include <cstring>3#include <cmath>4#include <algorithm>5#include <cstdio>6 using namespacestd;7 8 typedef __int64 LL;9 Ten ll T,x,n,y,m; One ll A, B; A  - ll GDC (ll A,ll b) { -     return(b>0)? GDC (b,a%b): A; the } -  - intMain () { -      while(cin>>t&&t) { +         intans=1; -          while(t--){ +Cin>>x>>n>>y>>m; AA=0; atb=x; -              for(intI=1; i<=n;i++){ -                 if(b>a) { -a+=b; -                 } -                 Else{ inb+=A; -                 } to             } +               -             if(a>=b) { thea+=y; *             } $             Else{Panax Notoginsengb+=y; -             } the              +              A ll sum; the             if(a>b) { +sum=GDC (A, b); -             } $             Else{ $sum=GDC (b,a); -             } -printf"Case %d:%i64d\n", ans,sum); theans++; -         }Wuyi     } the     return 0; -}

Game of Peace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.