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 要用到辗转相减的更多相关文章

  1. 【博弈+GCD】C. Alice and Bob

    https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/C [题意] 初始时有n个数,定义操作为从n个数中取出两个数x,y,如果|x-y| ...

  2. [Luogu1891]疯狂LCM[辗转相减法]

    题意 多组询问,每次给定 \(n\) ,求:\(\sum_{i=1}^nlcm(i,n)\) . \(\rm T \leq 3\times 10^4\ ,n \leq 10^6\). 分析 推式子: ...

  3. C语言复习---获取最大公约数(辗转相除法和更相减损法)

    源自:百度百科 辗转相除法 辗转相除法:辗转相除法是求两个自然数的最大公约数的一种方法,也叫欧几里德算法. 例如,求(,): ∵ ÷=(余319) ∴(,)=(,): ∵ ÷=(余58) ∴(,)=( ...

  4. (中等) CF 585C Alice, Bob, Oranges and Apples,矩阵+辗转相除。

    Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples ...

  5. Sicily 1732 Alice and Bob (二进制最大公约数)

    联系: http://soj.me/1732 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description: Alice is a b ...

  6. 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  7. bzoj4730: Alice和Bob又在玩游戏

    Description Alice和Bob在玩游戏.有n个节点,m条边(0<=m<=n-1),构成若干棵有根树,每棵树的根节点是该连通块内编号最 小的点.Alice和Bob轮流操作,每回合 ...

  8. Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...

  9. sdutoj 2608 Alice and Bob

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...

随机推荐

  1. MVC5-9 今天讲三个Descriptor

    ControllerDescriptor.ActionDescriptor.ParmaterDescriptor 看名字一脸懵逼,这是做什么的呢?可别小看它们,它们在MVC中扮演着尤为重要的角色. 思 ...

  2. Newtonsoft.Json动态过滤属性

    Newtonsoft.Json动态过滤属性 接口写的多了,会发现很多的问题.同一个dto,不同的action返回的字段个数不一样.往往开发人员因为懒或者各种原因一股脑的全返回,会浪费很多流量且用户体验 ...

  3. 原生JS中常用的Window和DOM对象操作汇总

    一.常用的Window对象操作 Window对象中又包含了document.history.location.Navigator和screen几个对象,每个对象又有自己的属性方法,这里window可以 ...

  4. AngularJs ngCloak、ngController、ngInit、ngModel

    ngCloak ngCloak指令是为了防止Angular应用在启动加载的时候html模板将会被短暂性的展示.这个指令可以用来避免由HTML模板显示造成不良的闪烁效果. 格式: ng-cloak   ...

  5. codevs 1051 接龙游戏(栈模拟)

    传送门 Description 给出了N个单词,已经按长度排好了序.如果某单词i是某单词j的前缀,i->j算一次接龙(两个相同的单词不能算接龙). 你的任务是:对于输入的单词,找出最长的龙. I ...

  6. [Android] HttpURLConnection & HttpClient & Socket

    Android的三种网络联接方式 1.标准Java接口:java.net.*提供相关的类//定义地址URL url = new URL("http://www.google.com" ...

  7. BZOJ3813: 奇数国

    传送门 欧拉函数+线段树 因为只有60个素数,所以把状态压成long long的形式.用线段树维护区间和和区间和中有多少个质数.然后xjb搞搞就行了,具体参见代码. //BZOJ 3813 //by ...

  8. HDU5671Matrix(矩阵行列交换)

    有一个nn行mm列的矩阵(1 \leq n \leq 1000 ,1 \leq m \leq 1000 )(1≤n≤1000,1≤m≤1000),在这个矩阵上进行qq (1 \leq q \leq 1 ...

  9. 一种M2M业务的架构及实现M2M业务的方法

    http://www.cnblogs.com/coryxie/p/3849764.html 技术领域 [0001] 本发明涉及通信技术领域,尤其涉及一种M2M业务的架构及实现M2M业务的方法. 背景技 ...

  10. 过滤字符串的Html标记 c#函数 .

    .public static string StripHTML(string strHtml) . { . string[] aryReg ={ . @"<script[^>]* ...