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. EF-CodeFirst 继承关系TPH、TPT、TPC

    继承关系 面向对象的三大特征之一:继承 ,在开发中起到了重要的作用.我们的实体本身也是类,继承自然是没有问题.下面开始分析 EF里的继承映射关系TPH.TPT.TPC 现在我们有这样一个需求,用户里要 ...

  2. centos7安装mplayer的方法

    首先是要获取源代码. 首先是主程序的源代码. 打开你的终端,按照我的命令一步一步来: cd Download svn checkout svn://svn.mplayerhq.hu/mplayer/t ...

  3. python中%和format

    两者都是格式化字符串用的,前者是比较老的版本,现在已经不推荐,后者更强大一些 % In [22]: print '%s' % 'hello world' hello world In [23]: pr ...

  4. Beta版本——第三次冲刺博客

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  5. 【Alpha阶段】第五次Scrum例会

    由于软工整个项目规划延期1周,我们将停止2天的Scrum,进行相应的修整 会议信息 时间:2016.10.21 22:30 时长:20min 地点:大运村1号公寓5楼楼道 类型:日常Scrum会议 个 ...

  6. Ubuntu下忘记MySQL密码重设方法

    今天在做Python的实验,要用到MySQL数据库,但是密码是啥捏~~~果断重新设置密码啦啦啦 http://www.cnblogs.com/yuxc/archive/2012/07/25/26075 ...

  7. 深入理解JavaScript中的属性和特性

    深入理解JavaScript中的属性和特性 JavaScript中属性和特性是完全不同的两个概念,这里我将根据自己所学,来深入理解JavaScript中的属性和特性. 主要内容如下: 理解JavaSc ...

  8. import_site

    http://kfd.me/ https://google.kfd.me/webhp?newwindow=1&safe=active http://googlebridge.com/searc ...

  9. JS-结合html综合练习js的对象——班级成绩表制作

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>对 ...

  10. 【原】javascript数组操作

    继续我的第二遍<javascript高级程序设计第三版>,今天要做的笔记是array 一.数组的操作 1.数组的创建: var colors= new Array(); //创建一个数组 ...