HUD 5050 Divided Land

http://acm.hdu.edu.cn/showproblem.php?pid=5050

题目大意:

  给定一个矩形的长和宽,把这个矩形分成若干相等的正方形,没有剩余。求正方形的边长最长是多少。

解题思路:

  这道题是“pick定理”的一个变种(不知道是pick定理,也可以退出结论)。由定理是求矩形的长和宽最大公约数。但是这道题,输入的数是二进制,

输出也是二进制,二进制数的大小为2^1000,所以是大数的算法。java有大数的处理。

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 
 5 public class Main {
 6     public static void main(String[] args) {
 7         Scanner cin = new Scanner(System.in);
 8 
 9         int t = cin.nextInt();
10         for(int cs = 1; cs <= t; ++cs){
11             BigInteger a = cin.nextBigInteger(2);
12             BigInteger b = cin.nextBigInteger(2);
13             System.out.println("Case #"+cs+": "+a.gcd(b).toString(2));
14         }
15 
16         cin.close();
17     }
18 }
原文地址:https://www.cnblogs.com/xuqiulin/p/3999124.html