HDU 5050 Divided Land (greatest common divisor Java)

Source: Internet
Author: User
Tags gcd greatest common divisor

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


Problem DescriptionIt'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 were both in binary form. As the mayor, you must segment the land into multiple squares of the equal size for the villagers. What was required is there must was no any waste and each of the segmented square land have as large area as possible. The width of the segmented square land is also binary.
Inputthe first line of the input was T (1≤T≤100), which stands for the number of test cases you need to solve.

Each case contains-binary number represents the length L and the width W of given land. (0 < L, w≤21000)
Outputfor, 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 the can is divided from input data. And it'll be show in binary. Do not has any useless number or space.
Sample Input
310 100100 11010010 1100

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

Source2014 ACM/ICPC Asia Regional Shanghai Online

Ps:

The idea is very simple, is to convert the input binary length and width into decimal to find a gcd and then in the conversion to binary output can, but because the data is too large, need to use Java to achieve, here stick a teammate knocking Java;


The code is as follows:

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));}}}         


HDU 5050 Divided Land (greatest common divisor Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.