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 }