HDU 5050 Divided Land(最大公约数Java)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5050

Problem Description

It’s time to fight the local despots and redistribute the land. There is a rectangular piece of land granted from the government, whose length and width are both in binary form. As the mayor, you must segment the land into multiple squares of equal size for
the villagers. What are required is there must be no any waste and each single segmented square land has as large area as possible. The width of the segmented square land is also binary.

Input

The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.

Each case contains two binary number represents the length L and the width W of given land. (0 < L, W ≤ 21000)

Output

For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then one number means the largest width of land that can be divided from input data. And it will be show in binary. Do not have any useless number
or space.

Sample Input

3
10 100
100 110
10010 1100

Sample Output

Case #1: 10
Case #2: 10
Case #3: 110

Source

2014 ACM/ICPC Asia Regional Shanghai Online

PS:

思路很简单,就是把输入的二进制长和宽转化为十进制求一个GCD然后在转换为二进制输出即可,不过由于数据过大,需要用Java来实现,这里贴一发队友敲的Java;

代码如下:

import java.math.*;
import java.util.Scanner;

public class Main{
    public static BigInteger gcd(BigInteger a,BigInteger b)
    {
        if(b.equals(BigInteger.ZERO))
            return a;
        return gcd(b,a.mod(b));
    }
        public static void main(String[] args)
       {
                 Scanner input = new Scanner(System.in);
                 int t,i,j;
                 String s=null;
                 char str[];
                 BigInteger a,b;
                 t=input.nextInt();
                 for(i=1;i<=t;i++)
                 {
                     a=input.nextBigInteger(2);
                     b=input.nextBigInteger(2);
                     a=gcd(a,b);
                     System.out.println("Case #"+i+": "+a.toString(2));
                 }
       }

}
时间: 2024-12-12 16:10:12

HDU 5050 Divided Land(最大公约数Java)的相关文章

HDU 5050 Divided Land ( JAVA )

HDU 5050 - Chinese Girls' Amusement ( JAVA or 高精度 ) 题意不用再解释 做法是求两个二进制数的最大公约数字 然后以二进制输出 import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static PrintWriter out = new PrintWriter(new BufferedWriter( new OutputStrea

HDU 5050 Divided Land(进制转换)

题意  给你两个二进制数m,n   求他们的最大公约数  用二进制表示  0<m,n<2^1000 先把二进制转换为十进制  求出最大公约数  再把结果转换为二进制  数比较大要用到大数 import java.util.*; import java.math.*; public class wl6_9 { static BigInteger two = BigInteger.valueOf(2), one = BigInteger.ONE, zero = BigInteger.ZERO; s

hdu 5050 Divided Land(JAVA高精度)

import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(System.in); int T; T=cin.nextInt(); BigInteger a,b; for(int cas=1;cas<=T;cas++){ a=cin.nextBigInteger(2); b=ci

hdu 5050 Divided Land

题目:本质是求两个数的最大公约数,java大数真好用 ^_^. import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { BigInteger TWO = BigInteger.valueOf(2); Scanner t = new Scanner(System.in); int cas = t.nextInt(); for

hdu 5050 Divided Land---2014acm上海赛区网络赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5050 Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 115    Accepted Submission(s): 57 Problem Description It's time to fight the loc

HUD 5050 Divided Land

http://acm.hdu.edu.cn/showproblem.php?pid=5050 题目大意: 给定一个矩形的长和宽,把这个矩形分成若干相等的正方形,没有剩余.求正方形的边长最长是多少. 解题思路: 这道题是“pick定理”的一个变种(不知道是pick定理,也可以退出结论).由定理是求矩形的长和宽最大公约数.但是这道题,输入的数是二进制, 输出也是二进制,二进制数的大小为2^1000,所以是大数的算法.java有大数的处理. 1 import java.math.BigInteger;

hdu----(5050)Divided Land(二进制求最大公约数)

Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 123    Accepted Submission(s): 64 Problem Description It’s time to fight the local despots and redistribute the land. There is a rect

HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)

在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平行)为,i!=j时,a1[j]=a2[j],且随机取区间[0,n-1]内的整数.然后a1[i],a2[i]在保证a1[i]<a2[i]的前提下同样随机. 由于D条线段各自跟自己维度的轴平行,我们可以转换成只求第i个维度与第j个维度的相交期望,然后乘以C(2,n)就好了 显然线段[a1,a2]和线段[

欧几里得求最大公约数--JAVA递归实现

欧几里得算法求最大公约数算法思想: 求p和q的最大公约数,如果q=0,最大公约数就是p:否则,p除以q余数为r,p和q的最大公约数即q和r的最大公约数. java实现代码: 1 public class Demo0 { 2 public static void main(String[] args) { 3 4 System.out.println(gcd(24,120)); 5 } 6 7 8 public static int gcd(int p,int q){ 9 10 if(q==0)