求2个二进制数的GCD
java大数+位压
import java.math.*;
import java.util.*;
import java.io.*;
public class Main {
public BigInteger GCD(BigInteger x, BigInteger y) {
if(x.compareTo(y) < 0) {
BigInteger tmp = x;
x = y;
y = tmp;
}
while(y.compareTo(BigInteger.ZERO) != 0) {
BigInteger tmp = x.mod(y);
x = y;
y = tmp;
}
return x;
}
public void work() {
int T, cas = 0;
String a = new String();
String b = new String();
char c[] = new char[1005];
T = cin.nextInt();
while (T-- > 0) {
a = cin.next();
b = cin.next();
BigInteger x = BigInteger.ZERO;
for(int i = 0; i < a.length(); i ++) {
x = x.multiply(BigInteger.valueOf(2));
if(a.charAt(i) == '1') x = x.add(BigInteger.ONE);
}
BigInteger y = BigInteger.ZERO;
for(int i = 0; i < b.length(); i ++) {
y = y.multiply(BigInteger.valueOf(2));
if(b.charAt(i) == '1') y = y.add(BigInteger.ONE);
}
// System.out.println(x + "," + y);
x = GCD(x, y);
int i = 0;
while(x.compareTo(BigInteger.ZERO) > 0) {
if( x.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO) == 0) {
c[i] = '0';
} else c[i] = '1';
i ++;
x = x.divide(BigInteger.valueOf(2));
}
cas ++;
System.out.print("Case #" + cas + ": ");
while(i > 0) {
System.out.print(c[i-1]);
i--;
}
System.out.println();
}
}
Main() {
cin = new Scanner(System.in);
}
public static void main(String[] args) {
Main e = new Main();
e.work();
}
public Scanner cin;
}