ICPC Arab Collegiate Programming Contest 2014

Problem A. Game of Peace

Program: numbers.(cpp|java)
Input: numbers.in
Balloon Color: Pink
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.
Examples
numbers.in stdout
2
4 3 6 5
5 1 15 2
Case 1: 2
Case 2: 5
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.

思路:通过分析前n段数据发现,加来加去他们的gcd并没有改变,所以后面的道理也一样,后面的m-n次加来加去并没有用,计算前n次,然后加个y 直接求gcd就好了,中间求解的过程可能会出现很大的数,用java大数写了一发

import java.math.BigInteger;
import java.util.*;
public class Main {
 
    public static void main(String[] args) {
        BigInteger x,y,tmp,s;
        int n,m,t ,j=1;
         Scanner input = new Scanner(System.in);
         t = input.nextInt();
        for( j=1;j<=t;j++)
        {
        x = input.nextBigInteger();  //输入一个大整数
        n = input.nextInt();
        y = input.nextBigInteger();
        m = input.nextInt();
        BigInteger z=new BigInteger("1");//赋初始值(方法一)
        BigInteger b=BigInteger.valueOf(0);//赋初始值(方法二)
        BigInteger a=x.add(b);  //大数加法
       for(int i=0;i<n;i++)
       {
          // System.out.println(a+"  "+b);
           if(a.compareTo(b)==0)    //比较大小
           {
               a=a.add(a);
           }
           else if(a.compareTo(b)>0)
           {
               tmp=a.multiply(z);
               b=b.add(tmp);
           }
           else if(a.compareTo(b)<0)
           {
               tmp=b.multiply(z);
               a=a.add(tmp);
           }
          
       }
       if(a.compareTo(b)>=0)
       {
           a=a.add(y);
       }
       else if(a.compareTo(b)<0)
       {
           b=b.add(y);
       }
      System.out.print("Case "+j+": ");
       System.out.println(a.gcd(b)); //大数GCD
    }
    }
}
View Code

 

posted @ 2020-07-29 21:47  Swelsh-corgi  阅读(151)  评论(0编辑  收藏  举报