HDU-5050 Divided Land (二进制求GCD)

题目大意:将两个二进制数的GCD用二进制数表示出来。

题目分析:这道题可以用java中的大数类AC。

代码如下:

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

public class Main {
	public static void main(String agrs[]){
		Scanner sc=new Scanner(System.in);
		int T=sc.nextInt();
		for(int i=1;i<=T;++i){
			String p=sc.next();
			String q=sc.next();
			BigInteger a=new BigInteger(p,2);
			BigInteger b=new BigInteger(q,2);
			a=a.gcd(b);
			System.out.print("Case #"+i+": ");
			System.out.println(a.toString(2));
		}
		sc.close();
	}
}

  

不过,也可以用二进制来求GCD。

gcd(a,b)=gcd(a/2,b/2)*2 (a,b均为偶数)

gcd(a,b)=gcd(a,b/2) (a为奇数,b为偶数)

gcd(a,b)=gcd((a-b)/2,b) (a,b均为奇数)

很可惜我用C++没AC,下面是我没AC的代码:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
# include<string>
using namespace std;

bool isBigger(string p,string q)
{
    if(p.length()>q.length()) return true;
    else if(p.length()<q.length()) return false;
    else{
        int len=p.length();
        for(int i=len-1;i>=0;--i){
            if(p[i]>q[i]) return true;
            else if(p[i]<q[i]) return false;
        }
    }
}

string sub(string p,string q)
{
    int len=q.length();
    for(int i=0;i<len;++i){
        if(p[i]>=q[i])
            p[i]=p[i]-q[i]+‘0‘;
        else{
            p[i+1]-=1;
            p[i]=p[i]+2-q[i]+‘0‘;
        }
    }
    for(int i=len;i<p.length();++i){
        if(p[i]<‘0‘&&i+1<p.length()){
            --p[i+1];
            p[i]+=2;
        }
    }
    len=p.length();
    while(p[len-1]<=‘0‘&&len>0)
        --len;
    return p.substr(0,len);
}

string f(string p,string q)
{
    if(p==q) return p;
    if(p=="1") return "1";
    if(q=="1") return "1";
    int n=p.length(),m=q.length();
    if(p[0]==‘0‘&&q[0]==‘0‘)
        return f(p.substr(1,n-1),q.substr(1,m-1))+‘0‘;
    else if(p[0]==‘1‘&&q[0]==‘0‘)
        return f(p,q.substr(1,m-1));
    else if(p[0]==‘0‘&&q[0]==‘1‘)
        return f(p.substr(1,n-1),q);
    else{
        if(isBigger(p,q)){
            p=sub(p,q);
            n=p.length();
            return f(p.substr(1,n-1),q);
        }else{
            q=sub(q,p);
            m=q.length();
            return f(p,q.substr(1,m-1));
        }
    }
}

int main()
{
    int T;
    string p,q;
    scanf("%d",&T);
    for(int i=1;i<=T;++i){
        cin>>p>>q;
        reverse(p.begin(),p.end());
        reverse(q.begin(),q.end());
        cout<<"Case #"<<i<<": "<<f(p,q)<<endl;
    }
    return 0;
}

  

时间: 2024-08-04 19:25:25

HDU-5050 Divided Land (二进制求GCD)的相关文章

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

题目链接: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.

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(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---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 1059 多重背包+二进制优化

Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16909    Accepted Submission(s): 4729 Problem Description Marsha and Bill own a collection of marbles. They want to split the collection