Time Limit: 1000ms, Special Time Limit:2500ms,Memory Limit:32768KB |
Total submit users: 47, Accepted users:27 |
Problem 13778 : 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 |
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. |
Problem Source |
ACM International Collegiate Programming Contest, Arab Collegiate Programming Contest 2014 Egypt, Sharm El Sheikh, November 16th, 2014
其实这一道题一看觉得其实就是一道挺简单的题目。。。。。。但是超级多坑啊-。-
1、就是。。。。。。你要注意它可以操作十万次。。。。。。 你一个瓶子里面的小球球很快就可以超出数据类型了。。。。。。所以说,这里暗示了我们是不能强算的。 这里有一个小要点(最后说hh)
3、0和别的数字的最大公因数怎么算呢?是这个数本身
a*b*c*d+e*f*g*a=e*f*g*a a(b*c*d+e*f*g)=e*f*g*a (假设y比x大),我们可以看到,这样子加来加去是不会改变最大公因数的。
所以说,这个就是最关键的地方。
代码稍后在评论贴出~~~ -------评论有字数限制~~ 还是在文章里面修改好了 #include<iostream> #include<stdio.h> using namespace std; long long gcd(long long a, long long b) { for (long long r = a%b; r != 0; r = a%b) { a = b; b = r; } return b; } int main() { int n = 0; while (cin >> n) { for (int i = 1; i <= n; i++) { long long container1 = 0; long long container2 = 0; long long after = 0; long long add = 0; long long all = 0; scanf("%I64d%I64d%I64d%I64d", &container2, &after, &add, &all); //cout<<container2<<after<<add<<all; if (all == 0) { cout << "Case " << i << ":" << " " << container2+add<< endl; continue; } if (after == 0) { cout << "Case " << i << ":" << " " << container2+add<< endl; continue; } for (int k = 0, now = 0; k < all; k++) { if (container2 > container1) { container1 = container1 + container2; now++; if (now == after) { container1 = container1 + add; break; } } else { container2 = container2 + container1; now++; if (now == after) { container2 = container2 + add; break; } } } long long tmp = 0; if (container2 > container1) { tmp = container1; container1 = container2; container2 = tmp; } // cout<<container1<<" "<<container2<<endl; cout << "Case " << i << ":" << " " << gcd(container1, container2) << endl; } } } |
Game of Peace
最新推荐文章于 2020-08-11 22:37:29 发布