博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sicily 1732 Alice and Bob (二进制最大公约数)
阅读量:5764 次
发布时间:2019-06-18

本文共 3644 字,大约阅读时间需要 12 分钟。

联系: 

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 
#include
#include
#include
#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
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
= 0) { //没有借位。 borrow = 0, N.v[i] = temp; }else { borrow = 1, N.v[i] = temp + 2; } } for(; i
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
=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;}

版权声明:本文博主原创文章,博客,未经同意不得转载。

你可能感兴趣的文章
看雪论坛502,出现安全宝?
查看>>
使用TMG配置×××注意事项
查看>>
使用PullToRefresh实现下拉刷新和上拉加载
查看>>
自适应轮播图
查看>>
mysql
查看>>
管家婆数据库823错误,并闩锁页错误数据恢复成功
查看>>
2012年电信业八大发展趋势
查看>>
Web日志安全分析工具 v2.0发布
查看>>
JS重载
查看>>
python2和python3同安装在Windows上,切换问题
查看>>
php加速工具xcache的安装与使用(基于LNMP环境)
查看>>
android超链接
查看>>
redhat tomcat
查看>>
C#如何提取PPT中 SmartArt文本和批注中的文本
查看>>
通过文本查找元素
查看>>
统计数据库大小
查看>>
Asp.net MVC3学习案例
查看>>
IO流的学习--文件夹下文件的复制
查看>>
第十六章:脚本化HTTP
查看>>
EXCEL表中如何让数值变成万元或亿元
查看>>