HDU5050Divided Land (java large number)

Source: Internet
Author: User

HDU5050Divided Land (java large number)

HDU5050Divided Land (java large number)

Question Link

A long and wide rectangle requires you to divide the area into multiple squares with N sides and there is no space left. Require N to be as big as possible. Given the length and width of binary, the output N is also in binary format.

Solution: Find the GCD of L and M. However, because the input of this question is a very large binary number, it is only in the middle of the GCD to convert to decimal to obtain N, and the output N is also in the binary form.

Code:

import java.util.*;import java.io.*;import java.math.*;public class Main {    public static BigInteger GCD (BigInteger num1, BigInteger num2) {        if (num2.equals(BigInteger.ZERO))            return num1;        return GCD(num2, num1.mod(num2));    }    public static void main(String args[]) {        Scanner cin = new Scanner(new BufferedInputStream(System.in));        PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));        int T;        T = cin.nextInt();        BigInteger L, W;        for (int i = 1; i <= T; i++) {            L = cin.nextBigInteger(2);            W = cin.nextBigInteger(2);            BigInteger ans = GCD(L, W);            cout.printf("Case #%d: ", i);            cout.println(ans.toString(2));        }        cin.close();        cout.close();    }}

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.