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. Git工具:Widows下的使用(提交到Github)

    2016年12月9日17:07:07 Git工作原理 http://deweixu.me/2016/11/05/how-git-works/ 2016年12月1日14:25:23 ---------- ...

  2. Linux 之 编译器 gcc/g++参数详解

    2016年12月9日16:48:53 ----------------------------- 内容目录: [介绍] gcc and g++分别是gnu的c & c++编译器 gcc/g++ ...

  3. HDU5670Machine(抽象进制)

    有一个机器,它有 m (2\leq m\leq 30)m(2≤m≤30) 个彩灯和一个按钮.每按下按钮时,最右边的彩灯会发生一次变换.变换为: 1. 如果当前状态为红色,它将变成绿色: 2.如果当前状 ...

  4. UVA11178 Morley's Theorem(基础模板)

    题目链接 题意:给出A,B, C点坐标求D,E,F坐标,其中每个角都被均等分成三份   求出 ABC的角a, 由 BC 逆时针旋转 a/3 得到BD,然后 求出 ACB 的角a2, 然后 由 BC顺时 ...

  5. SQL Server编程(03)自定义存储过程

    存储过程是一组预编译的SQL语句,它可以包含数据操纵语句.变量.逻辑控制语句等. 存储过程允许带参数: 输入参数:可以在调用时向存储过程传递参数,此类参数可用来向存储过程中传入值(可以有默认值) 输出 ...

  6. linux 查看内存的插槽数

    [root@web ~]# dmidecode|grep -P -A5 "Memory\s+Device"| grep Size | grep -v Range #linux查看内 ...

  7. Java递归算法——二分查找

    import java.lang.reflect.Array; import java.nio.Buffer; import java.util.Arrays; import java.util.Ra ...

  8. css input checkbox和radio样式美化

    参考:https://segmentfault.com/a/1190000004553258 http://www.haorooms.com/post/css_mh_ck_radio 思路都一样的,先 ...

  9. Effective Objective-C 2.0 — 第13条:用“方法调配 技术” 调试 “黑盒方法”

    自己理解是调配了方法 在运行期,可以向类中新增或替换选择子所对应的方法实现. 使用另一份实现来替换原有的方法实现,这道工序叫做“方法调配”,开发者常用此技术向原有实现中添加新功能. 一般来说,只有调试 ...

  10. Python开发【第四篇】:Python基础之函数

    三元运算 三元运算(三目运算),是对简单的条件语句的缩写. ? 1 2 3 4 5 # 书写格式   result = 值1 if 条件 else 值2   # 如果条件成立,那么将 “值1” 赋值给 ...