The main topic: The GCD of two binary numbers is represented by a binary number.
Topic Analysis: This problem can be used in Java in the large number of AC.
The code is as follows:
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 ();}}
However, you can also use binary to find GCD.
GCD (A, B) =gcd (A/2,B/2) * (A, b all are even)
GCD (b) =gcd (A,B/2) (A is odd, B is even)
GCD (A, B) =gcd ((a)/2,b) (A, b are odd)
Unfortunately I use C + + without AC, here is my code without 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;}
HDU-5050 divided Land (binary seeking GCD)