您的位置:首页 > 编程语言 > C语言/C++

Game of Peace

2017-07-30 13:27 99 查看
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)

2、要注意。。。。。。有没有想过操作0次的情况呢?也就是说我们只会出现一次bug后进行输出。

3、0和别的数字的最大公因数怎么算呢?是这个数本身

4、想到了一开始我提到了不能强算的那个留下的一个要点吗~嘻嘻(●'◡'●)其实就是说,当你出现了第一次bug之后就可以不用算了,因为后续的操作对两个瓶子里面的球球的最大公因数并没有影响~我们假设瓶子1里面有x个球球 x=a*b*c*d先这样子做一下假设,不一定有四项其中能知道的就是,x可以拆分成素数相乘瓶子2里面有y个球球,y=e*f*g*a,其中也是做一下假设而已~不一定有四项,其中能知道的就是,y可以拆分成素数相乘

我们按照题目这样子clone,每次能做的,就是。

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大),我们可以看到,这样子加来加去是不会改变最大公因数的。

唯一可能出现变化的,就是出的那次bug额外加的球球。

所以说,这个就是最关键的地方。

代码稍后在评论贴出~~~

-------评论有字数限制~~

还是在文章里面修改好了

#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;
             
4000
   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;
        }
    }
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM oj c++语言