Alice and Bob 要用到辗转相减

2023-06-25,,

Alice and BobTime Limit: 1 Sec  Memory Limit: 64 MB
Submit: 255  Solved: 43

Description

Alice is a beautiful and clever girl. Bob would like to play with Alice. One day, Alice got a very big rectangle and wanted to divide it into small square pieces. Now comes a problem: if all pieces of small squares are of the same size, how big could the squares be? To Alice, it's easy to solve the problem. However, she was very busy, so she asked Bob to help her. You know Alice is such a lovely girl and of course Bob won't refuse her request. But Bob is not so smart and he is especially weak in math. So he turns to you —a genius at programming. Alice will inform Bob the length and width of the big rectangle, and Bob have to tell her the longest length for the small square. All of these numbers are in their binary representations.

Input

The first line of the input is a positive integer. This is the number of the test cases followed. Each test case contains two integer L and W in their binary representation which tells you the length and width of the very big rectangle(0< L , W < 2^1000 ).There may be one or several spaces between these integers.

Output

The output of the program should consist of one line of output for each test case. The output of each test case only contains the longest length for the small squares in its binary representation. No any redundant spaces are needed.

Sample Input

2
100 1000
100 110

Sample Output

100
10 本来想用辗转相除法,但问大神后据说会超时,就用了辗转相减

若a,b都是偶数,则gcd(a,b)=2*gcd(a/2,b/2);

若a是奇数,b是偶数,则gcd(a,b)=gcd(a,b/2);

若a,b都是奇数,则gcd(a,b)=gcd(a-b,b); 注意以上3条都要用到,单用第3条会超时。
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
void mult(int p[],int x,int *len1)
{
int i,temp=;
for(i=;i<*len1;i++)
{
p[i]=p[i]*x+temp;
temp=p[i]/;
p[i]%=;
}
if(temp)
{
p[i]=temp;
(*len1)++;
}
} void add(int p[],int q[],int *len1)
{
int i;
for(i=;i<*len1;i++)
{
p[i]+=q[i];
if(p[i]>)
{
p[i]-=;
p[i+]++;
}
}
if(p[i])
(*len1)++;
} void diviT(int p[],int *len,int *rem) //'rem' is used for installing remainder
{
int m=,i;
for(i=*len-;i>=;i--)
{
m=m*+p[i];
if(!(m/))
{
p[i]=;
}
else
{
p[i]=m/;
m=m%;
}
}
for(i=*len-;i>=;i--)
if(p[i])
{
*len=i+;
break;
} if(i<)
*len=;
*rem=m;
} void SubStract(int p1[],int p2[],int *len1,int *len2) //辗转相减
{
int i,flag=,len; if(p1[]%==)
{
len=*len1;
diviT(p1,&len,&i);
*len1=len;
}
if(p2[]%==)
{
len=*len2;
diviT(p2,&len,&i);
*len2=len;
}
if(*len1>*len2) flag=;
else if(*len1<*len2) flag=;
else
for(i=*len1-;i>=;i--)
if(p1[i]>p2[i])
{
flag=;
break;
}
else if(p1[i]<p2[i])
{
flag=;
break;
}
if(flag==)
{
for(i=(*len1)-;i>=*len2;i--)
p2[i]=; for(i=;i<*len1;i++)
{
p1[i]-=p2[i];
if(p1[i]<)
{
p1[i]+=;
p1[i+]--;
}
} if(p1[i-]==)
(*len1)--;
SubStract(p1,p2,len1,len2);
}
else if(flag==)
{
for(i=(*len2)-;i>=*len1;i--)
p1[i]=; for(i=;i<*len2;i++)
{
p2[i]-=p1[i];
if(p2[i]<)
{
p2[i]+=;
p2[i+]--;
}
} if(p2[i-]==)
(*len2)--;
SubStract(p1,p2,len1,len2);
}
else
return;
} int main() //transfer binary to dimcal ,convey double"长度,十进制的数组"
{
//freopen("a.txt","r",stdin);
int n,i,j;
char str1[];
char str2[];
int num_a[];
int num_b[];
int num_c[]; //两次出始化
int len1,len2; scanf("%d",&n); while(n--)
{
int mean=,k=;
scanf("%s",str1);
scanf("%s",str2); len2=len1=;
memset(num_a,,sizeof(num_a));
memset(num_b,,sizeof(num_b));
memset(num_c,,sizeof(num_c)); for(i=;str1[i]!='\0';i++)
{
num_c[]=str1[i]-'';
mult(num_a,,&len1); //乘2
add(num_a,num_c,&len1);
}
for(i=;str2[i]!='\0';i++)
{
num_c[]=str2[i]-'';
mult(num_b,,&len2);
add(num_b,num_c,&len2);
}
num_c[]=;
/* printf("num_a:");
for(i=len1-1;i>=0;i--)
printf("%d",num_a[i]);
printf("\n");
printf("num_b:");
for(i=len2-1;i>=0;i--)
printf("%d",num_b[i]);
printf("\n"); */
while() //为辗转相减做准备
{
if(num_a[]%==&&num_b[]%==)
{
mean*=;
diviT(num_a,&len1,&j);
diviT(num_b,&len2,&j);
}
else
break;
}
// printf("mean=%d\n",mean);
SubStract(num_a,num_b,&len1,&len2);
if(mean!=)
mult(num_a,mean,&len1);
/* for(i=len1-1;i>=0;i--)
printf("%d",num_a[i]);
printf("\n");*/
while()
{
diviT(num_a,&len1,&j);
for(i=len1-;i>=&&num_a[i]==;i--);
if(i>=)
num_c[k++]=j;
else
{
num_c[k++]=j;
break;
} }
for(i=k-;i>=;i--)
printf("%d",num_c[i]);
printf("\n");
}
return ;
}

Alice and Bob 要用到辗转相减的相关教程结束。

《Alice and Bob 要用到辗转相减.doc》

下载本文的Word格式文档,以方便收藏与打印。