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. Oracle 应用于.NET平台

    1. 回顾ADO.NET ADO.NET是一组用于和数据源进行交互的面向对象类库集,它存在于.Net Framework中.通常情况下,数据源可以是各种类型的数据库,利用ADO.NET可以访问目前几乎 ...

  2. ( 译、持续更新 ) JavaScript 上分小技巧(二)

    考虑到文章过长,不便于阅读,这里分出第二篇,如有后续,每15个知识点分为一篇... 第一篇地址:( 译.持续更新 ) JavaScript 上分小技巧(一) 第三篇地址:( 译.持续更新 ) Java ...

  3. DropZone

    JavaScript 文件拖拽上传插件 dropzone.js 介绍 February 19, 2014 / 编程指南 dropzone.js 是一个开源的 JavaScript 库,提供 AJAX ...

  4. 请求servlet操作成功后,在JSP页面弹出提示框

    应用环境: 点击前台页面,执行某些操作.后台action/servlet 执行后,返回处理结果(成功.失败.原因.状态等)信息.在前台jsp进行弹窗显示,alert(); 后台处理代码:(把要提示的数 ...

  5. linux下git的简单运用

    linux下git的简单运用 windows下也有git,是git公司出的bash,基本上模拟了linux下命令行.许多常用的命令和linux下操作一样.也就是说,windows下的git命令操作和l ...

  6. web前端环境搭建

    第一部分:浏览器 浏览器推荐chrome浏览器.FireFox浏览器. 1. chrome浏览器因为集成了Google Developer Tools(谷歌开发者工具),因此大受欢迎. 下载地址:ht ...

  7. 淘淘商城基于maven和svn的理解

    首先了解下maven和svn是什么: Maven是一个项目的管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目的生命周期(Project Life ...

  8. 《Java疯狂讲义》(第3版)学习笔记 2 - Java语言的运行机制

    内容 1.高级语言的运行机制 2.Java 语言的运行机制 1.高级语言的运行机制 高级语言主要分为编译型语言和解释型语言两类. 编译型语言是指使用专门的编译器.针对特定平台(操作系统)将高级语言源代 ...

  9. css005 用层叠管理多样式

    css005 用层叠管理多样式 当一个元素继承多个样式时,最近的祖先样式胜出(通俗一点就是自己有就用自己的,自己没有找parent,parent没有找grandprent,再没有就一级一级网上找) 当 ...

  10. easyUI数据表格datagrid之分页

    一.分页函数 /**========================================= * 分页函数 */function pagerFilter(data) { if(typeof ...