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

二进制求最大公约数:

代码:

  1 #include <stdio.h>
  2 #include <string.h>
  3 #define MAXN 1000
  4 struct BigNumber{
  5    int len;
  6    int v[MAXN];
  7 };
  8 bool isSmaller(BigNumber n1,BigNumber n2)
  9 {
 10  if(n1.len<n2.len)
 11   return 1;
 12  if(n1.len>n2.len)
 13   return 0;
 14  for(int i=n1.len-1;i>=0;i--)
 15  {
 16   if(n1.v[i]<n2.v[i])
 17    return 1;
 18   if(n1.v[i]>n2.v[i])
 19    return 0;
 20  }
 21  return 0;
 22 }
 23 BigNumber minus(BigNumber n1,BigNumber n2)
 24 {
 25  BigNumber ret;
 26  int borrow,i,temp;
 27  ret=n1;
 28  for(borrow=0,i=0;i<n2.len;i++)
 29  {
 30   temp=ret.v[i]-borrow-n2.v[i];
 31   if(temp>=0)
 32   {
 33    borrow=0;
 34    ret.v[i]=temp;
 35   }
 36   else
 37   {
 38    borrow=1;
 39    ret.v[i]=temp+2;
 40   }
 41  }
 42  for(;i<n1.len;i++)
 43  {
 44   temp=ret.v[i]-borrow;
 45   if(temp>=0)
 46   {
 47    borrow=0;
 48    ret.v[i]=temp;
 49   }
 50   else
 51   {
 52    borrow=1;
 53    ret.v[i]=temp+2;
 54   }
 55  }
 56  while(ret.len>=1 && !ret.v[ret.len-1])
 57   ret.len--;
 58  return ret;
 59 }
 60 BigNumber div2(BigNumber n)
 61 {
 62  BigNumber ret;
 63  ret.len=n.len-1;
 64  for(int i=0;i<ret.len;i++)
 65   ret.v[i]=n.v[i+1];
 66  return ret;
 67 }
 68 void gcd(BigNumber n1,BigNumber n2)
 69 {
 70  long b=0,i;
 71  while(n1.len && n2.len)
 72  {
 73   if(n1.v[0])
 74   {
 75    if(n2.v[0])
 76    {
 77     if(isSmaller(n1,n2))
 78      n2=minus(n2,n1);
 79     else
 80      n1=minus(n1,n2);
 81    }
 82    else
 83     n2=div2(n2);
 84   }
 85   else
 86   {
 87    if(n2.v[0])
 88     n1=div2(n1);
 89    else
 90    {
 91     n1=div2(n1);
 92     n2=div2(n2);
 93     b++;
 94    }
 95   }
 96  }
 97  if(n2.len)
 98   for(i=n2.len-1;i>=0;i--)
 99    printf("%d",n2.v[i]);
100  else
101   for(i=n1.len-1;i>=0;i--)
102    printf("%d",n1.v[i]);
103  while(b--)
104   printf("0");
105  printf("\n");
106 }
107 int main()
108 {
109  int cases,le,i;
110  BigNumber n1,n2;
111  char str1[MAXN],str2[MAXN];
112  scanf("%d",&cases);
113  for(int w=1;w<=cases;w++)
114  {
115   scanf("%s%s",str1,str2);
116   le=strlen(str1);
117   n1.len=le;
118   for(i=0;i<le;i++)
119    n1.v[i]=str1[le-1-i]-‘0‘;
120   le=strlen(str2);
121   n2.len=le;
122   for(i=0;i<le;i++)
123    n2.v[i]=str2[le-1-i]-‘0‘;
124    printf("Case #%d: ",w);
125   gcd(n1,n2);
126  }
127  return 0;
128 }

时间: 2025-01-02 14:36:52

hdu----(5050)Divided Land(二进制求最大公约数)的相关文章

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 )

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(最大公约数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

二进制求最大公约数&amp;&amp;输出二进制

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

HUD 5050 Divided Land

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

hdu5050 Divided Land(二进制最大公约数+java强大的读写功能)

题目链接:点击打开链接 题目描述:给定两串二进制,求其最大公约数,并以二进制的形式输出? 解题思路:java大整数 1.首先题目数据范围非常大,明显要用大整数 2.题目的输入和输出都是二进制,这个可以使用java方便的完成 3.求最大公约数,gcd即可,其实java大整数里面已经有这个函数了直接调用就行 代码 import java.math.BigInteger; import java.util.Scanner; public class Main { private static Scan