Divided Land
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 402 Accepted Submission(s): 213
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 ≤ 2 1000)
Each case contains two binary number represents the length L and the width W of given land. (0 < L, W ≤ 2 1000)
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
Source
这个题意是两个二进制数求他的最大公约数。
我用C++想到的方法就只有更相相减数,结果超时了。所以只能用java试试。据说C++有很好的方法,下次学习下。mark!
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define EPS 1e-8
using namespace std;
string s1,s2;
int pd(string a,string b)
{
int l1=a.length();
int l2=b.length();
if (l1<l2) return 1;
if (l1>l2) return 0;
for (int i=0;i<l1;i++)
{
if (s1[i]>s2[i]) return 0;
if (s1[i]<s2[i]) return 1;
}
}
void go(string a,string b)
{
int l=b.length();
int l1=a.length()-1;
for (int i=l-1;i>=0;i--)
{
a[l1]='0'+a[l1]-b[i];
// cout<<a<<"@"<<i<<endl;
if (a[l1]=='/')
{
//cout<<"@@@"<<endl;
int j=l1;
while (a[j]=='/')
{
a[j]='1';
a[j-1]='0'+a[j-1]-b[i];
j--;
}
}
l1--;
}
int i=0;
while (a[i]=='0') i++;
//cout<<a<<endl;
a.assign(a,i,a.length()-i);
//cout<<a<<"*"<<b<<endl;
int g=pd(a,b);
if (g==0)
{
s1=a;
s2=b;
}
else
{
s1=b;
s2=a;
}
}
int main()
{
int l1,l2,T,cas=1;
scanf("%d",&T);
while (T--)
{
getchar();
cin>>s1>>s2;
l1=s1.length();
l2=s2.length();
//cout<<s1<<" "<<s2<<endl;
while (s1!=s2 && s2!="" && s1!="")
{
int f=pd(s1,s2);
if (f) go(s2,s1);
else go(s1,s2);
//cout<<s1<<" "<<s2<<endl;
}
printf("Case #%d: ",cas++);
cout<<s1<<endl;
}
return 0;
}
package hdu5050;
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;
else 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);
System.out.println(a);
a=gcd(a,b);
System.out.println("Case #"+i+": "+a.toString(2));
}
}
}