Sicily 1732 Alice and Bob (二进制最大公约数)
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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
分析:本题的大意就是给出两个数的二进制。求出他们的最大公约数,要用辗转相除法,因为本题的数据范围较大,须要使用高精度,假设简单套用使用辗转相除法gcd(n, m) = gcd(m, n%m)来求的话,那么就要完毕一个高精度的除法的程序;
由于本题的输入和输出都使用二进制表示。所以能够使用下面方法来求最大公约数,(仅仅须要用高精度的除法和以为运算);
本题採用的算法例如以下:
if a = 2p, b = 2q, then gcd(a, b) = 2*gcd(p, q);
if a = 2p, b = 2q+1, then gcd(a, b) = gcd(p, b);
if a = 2p+1, b = 2q, then gcd(a, b) = gcd(a, q);
if a = 2p+1, b = 2q+1, then gcd(a, b) = gcd(a-b, b) (assume a > b)
容易看出前三种情况都会导致当中一个整数减半,这样递减的速度是非常快的,并且因为输入的是以二进制的方式输入,推断a, b的方式非常easy;
那会不会连续调用第四种情况呢?答案是不会的。原因是:
当a = 2p+1, b = 2q+1时:
gcd(a, b) = gcd(a-b, b) = gcd(2(p-q), 2q+1) = gcd(p-q, 2q+1);
明显不可能出现连续调用第四种情况,时间复杂度也和标准的转转相除法一样是O(logn);
代码例如以下:
// Problem#: 1732
// Submission#: 2822044
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define MAXN 10005
#define RST(N)memset(N, 0, sizeof(N))
using namespace std; typedef struct Node_
{
int len;
int v[MAXN];
}Node; Node n, m;
int cas;
char str1[MAXN], str2[MAXN]; Node Tr(char *str) //把字符串转换成数字形式;
{
Node N;
int len = strlen(str);
N.len = len;
for(int i=0; i<len; i++) N.v[i] = str[len-1-i]-'0';
return N;
} bool CMP(Node n, Node m) //比較两个数的大小;
{
if(n.len < m.len) return true;
if(n.len > m.len) return false;
for(int i=n.len-1; i>=0; i--) {
if(n.v[i] < m.v[i]) return true;
else if(n.v[i] > m.v[i]) return false;
}
return false;
} Node Minus(Node n, Node m) //大整数高精度相减。注意是二进制相减;
{
Node N = n;
int borrow = 0, temp, i; //borrow为借位;
for(i=0; i<m.len; i++) { //从低位减起;
temp = N.v[i] - borrow - m.v[i];
if(temp >= 0) { //没有借位。
borrow = 0, N.v[i] = temp;
}else {
borrow = 1, N.v[i] = temp + 2;
}
}
for(; i<n.len; i++) { //处理剩余位数;(如果n > m)
temp = N.v[i] - borrow;
if(temp >= 0) {
borrow = 0, N.v[i] = temp;
}else {
borrow = 1, N.v[i] = temp + 2;
}
}
while(N.len >= 1 && !N.v[N.len-1]) N.len--;
return N;
} Node div(Node n) //大整数除2;因为是二进制,其本质就是移位;
{
Node ret;
ret.len = n.len-1;
for(int i=0; i<ret.len; i++) ret.v[i] = n.v[i+1];
return ret;
} void gcd(Node n, Node m) //求大整数的公约数;
{
long cnt = 0;
while(n.len && m.len) {
if(n.v[0]) {
if(m.v[0]) { //a = 2p+1, b = 2q+1 情况
if(CMP(n, m)) m = Minus(m, n);
else n = Minus(n, m);
}else m = div(m); //a = 2p+1, b = 2q情况;
}else {
if(m.v[0]) n = div(n); //a = 2p, b = 2q+1情况。
else {
n = div(n), m = div(m); //a = 2p, b = 2q情况。
cnt++;
}
}
}
if(m.len) for(int i=m.len-1; i>=0; i--) printf("%d", m.v[i]); //输出结果;
else for(int i=n.len-1; i>=0; i--) printf("%d", n.v[i]);
while(cnt--) printf("0");
printf("\n");
} int main()
{
scanf("%d", &cas);
while(cas--) {
scanf("%s %s", str1, str2);
n = Tr(str1), m = Tr(str2);
gcd(n, m);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
Sicily 1732 Alice and Bob (二进制最大公约数)的更多相关文章
- CodeForces 346A Alice and Bob (数学最大公约数)
题意:有一堆数,然后有两个人轮流从中取出两个数,这两个数的差的绝对值不在这个集合,然后把这个数放进这个集合,如果哪个人不能拿了,就是输了,问你谁赢. 析:当时连题意都没看好,以为拿出两个数,就不放回了 ...
- SDUT 2608 Alice and Bob (巧妙的二进制)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...
- Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...
- sdutoj 2608 Alice and Bob
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...
- 位运算 2013年山东省赛 F Alice and Bob
题目传送门 /* 题意: 求(a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1) 式子中,x的p次方的系数 二进制位运算:p ...
- 2013年山东省第四届ACM大学生程序设计竞赛 Alice and Bob
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very ...
- ny788 又见Alice and Bob
又见Alice and Bob 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 集训生活如此乏味,于是Alice和Bob发明了一个新游戏.规则如下:首先,他们得到一个 ...
- 2013年山东省第四届ACM大学生程序设计竞赛E题:Alice and Bob
题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynom ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
随机推荐
- Android开发被添加到桌面快捷方式
Android开发被添加到桌面快捷方式 对于一个希望拥有很多其它用户的应用来说.用户桌面能够说是全部软件的必争之地,假设用户在手机桌面上建立了该软件的快捷方式.用户将会更频繁地使用该软件. 因此,全部 ...
- 8.Eclipse中创建Maven Web项目
第一步: 创建maven webproject 注意以下一步: 第二步: 继承parent 改动pom.xml文件例如以下 <projectxmlns="http://maven ...
- 【Linux探索之旅】开宗明义+第一部分第一课:什么是Linux?
内容简介 1.课程大纲 2.第一部分第一课:什么是Linux? 3.第一部分第二课预告:下载Linux,免费的噢! 开宗明义 我们总听到别人说:Linux挺复杂的,是给那些追求逼格的程序员用的.咱 ...
- OA项目设计的能力③
1.然后来了一个,写在我们的主要要求之一,有回波数据还需要添加的方法,我们需要知道,事实上,页被传递id演出id通讯实体name,所以想要回显就是须要得到privilegeIds,假设像上一篇在jsp ...
- zabbix 实现curl 显示器
1.进入Configure->Templates 2. 新建一个模板 3.新建模板,并保存 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFpND ...
- 探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据
上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...
- MVC 01
ASP.NET MVC 01 - ASP.NET概述 本篇目录: ASP.NET 概述 .NET Framework 与 ASP.NET ASP.NET MVC简介 ASP.NET的特色和优势 典型案 ...
- Cocos2d-x3.2游戏的核心循环在Application,怎样处理FPS不稳
今天天气非常阴,立即要下雨了,陈吃早点功夫写点东西, 一场秋雨一场寒,十场秋雨要穿棉,各位从今往后多穿点 int Application::run() { if(!applicationDidFini ...
- Android开发学习总结——Android开发的一些相关概念(转)
一.什么是3G.4G 1995年问世的第一代模拟制式手机(1G)只能进行语音通话. 1996到1997年出现的第二代GSM.CDMA等数字制式手机(2G)便增加了接收数据的功能 3G指的是第三代移 ...
- 2014年度辛星完全解读html部分
接下来,我们继续学习HTML标签,希望大家可以再接再厉.同一时候辛星也会支持大家.我们一起努力,一起加油. 我们本小节来认识另外几个标签. *************空格和换行************ ...