hdu----(5050)Divided Land(二进制求最大公约数)
Divided Land
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 123 Accepted Submission(s): 64
is a rectangular piece of land granted from the government, whose
length and width are both in binary form. As the mayor, you must segment
the land into multiple squares of equal size for the villagers. What
are required is there must be no any waste and each single segmented
square land has as large area as possible. The width of the segmented
square land is also binary.
Each case contains two binary number represents the length L and the width W of given land. (0 < L, W ≤ 21000)
the index of the test case) at the beginning. Then one number means the
largest width of land that can be divided from input data. And it will
be show in binary. Do not have any useless number or space.
10 100
100 110
10010 1100
Case #2: 10
Case #3: 110
#include <stdio.h>
#include <string.h>
#define MAXN 1000
struct BigNumber{
int len;
int v[MAXN];
};
bool isSmaller(BigNumber n1,BigNumber n2)
{
if(n1.len<n2.len)
return ;
if(n1.len>n2.len)
return ;
for(int i=n1.len-;i>=;i--)
{
if(n1.v[i]<n2.v[i])
return ;
if(n1.v[i]>n2.v[i])
return ;
}
return ;
}
BigNumber minus(BigNumber n1,BigNumber n2)
{
BigNumber ret;
int borrow,i,temp;
ret=n1;
for(borrow=,i=;i<n2.len;i++)
{
temp=ret.v[i]-borrow-n2.v[i];
if(temp>=)
{
borrow=;
ret.v[i]=temp;
}
else
{
borrow=;
ret.v[i]=temp+;
}
}
for(;i<n1.len;i++)
{
temp=ret.v[i]-borrow;
if(temp>=)
{
borrow=;
ret.v[i]=temp;
}
else
{
borrow=;
ret.v[i]=temp+;
}
}
while(ret.len>= && !ret.v[ret.len-])
ret.len--;
return ret;
}
BigNumber div2(BigNumber n)
{
BigNumber ret;
ret.len=n.len-;
for(int i=;i<ret.len;i++)
ret.v[i]=n.v[i+];
return ret;
}
void gcd(BigNumber n1,BigNumber n2)
{
long b=,i;
while(n1.len && n2.len)
{
if(n1.v[])
{
if(n2.v[])
{
if(isSmaller(n1,n2))
n2=minus(n2,n1);
else
n1=minus(n1,n2);
}
else
n2=div2(n2);
}
else
{
if(n2.v[])
n1=div2(n1);
else
{
n1=div2(n1);
n2=div2(n2);
b++;
}
}
}
if(n2.len)
for(i=n2.len-;i>=;i--)
printf("%d",n2.v[i]);
else
for(i=n1.len-;i>=;i--)
printf("%d",n1.v[i]);
while(b--)
printf("");
printf("\n");
}
int main()
{
int cases,le,i;
BigNumber n1,n2;
char str1[MAXN],str2[MAXN];
scanf("%d",&cases);
for(int w=;w<=cases;w++)
{
scanf("%s%s",str1,str2);
le=strlen(str1);
n1.len=le;
for(i=;i<le;i++)
n1.v[i]=str1[le--i]-'';
le=strlen(str2);
n2.len=le;
for(i=;i<le;i++)
n2.v[i]=str2[le--i]-'';
printf("Case #%d: ",w);
gcd(n1,n2);
}
return ;
}
hdu----(5050)Divided Land(二进制求最大公约数)的更多相关文章
- HDU 5050 Divided Land(进制转换)
题意 给你两个二进制数m,n 求他们的最大公约数 用二进制表示 0<m,n<2^1000 先把二进制转换为十进制 求出最大公约数 再把结果转换为二进制 数比較大要用到大数 ...
- Hdu 5050 Divided Land
题目要求就是做求两个二进制数的gcd,如果是用java的话,这题很简单.但也可以用C++做,只能先给自己留下这个坑了,还在研究c++的做法. import java.math.BigInteger; ...
- 二进制求最大公约数&&输出二进制
Divided Land Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- HUD 5050 Divided Land
http://acm.hdu.edu.cn/showproblem.php?pid=5050 题目大意: 给定一个矩形的长和宽,把这个矩形分成若干相等的正方形,没有剩余.求正方形的边长最长是多少. 解 ...
- HDU - 5050 (大数二进制gcd)
It's time to fight the local despots and redistribute the land. There is a rectangular piece of land ...
- 一个好的函数(gcd)求最小公约数
这个函数是我无意中看到的很不错,很给力,我喜欢 是用于求最小公约数的 简单的描述就是,记gcd(a,b)表示非负整数a,b的最大公因数,那么:gcd(a,b)=gcd(b,a%b)或者gcd(a,0) ...
- HDU 2503 a/b + c/d(最大公约数与最小公倍数,板子题)
话不多说,日常一水题,水水更健康!┗|`O′|┛ 嗷~~ a/b + c/d Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768 ...
- Euclid求最大公约数
Euclid求最大公约数算法 #include <stdio.h> int gcd(int x,int y){ while(x!=y){ if(x>y) x=x-y; else y= ...
- 算法:欧几里得求最大公约数(python版)
#欧几里得求最大公约数 #!/usr/bin/env python #coding -*- utf:8 -*- #iteration def gcd(a,b): if b==0: return a e ...
随机推荐
- Quartz:ERROR threw an unhandled Exception
详细的错误信息如下: -- ::] ERROR org.quartz.core.JobRunShell: - Job group1.job1 threw an unhandled Exception: ...
- 关于JDK的配置
① 安装官网下载的相应JDK安装包. 现在官网主推JDK8,JDK7以及更老的版本需要注册才能提供下载链接. ② 比如个人下载的jdk7-xxx.exe,安装路径为C:\Program Files\J ...
- Using GET_GROUP_SELECTION For Record Groups in Oracle Forms
Retrieves the sequence number of the selected row for the given group. Suppose you want to get a par ...
- Using Post-Form Trigger In Oracle Forms
Post-Form trigger in Oracle Forms fires during the Leave the Form process, when a form is exited. ...
- application 网站计数器
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Java编译环境的搭建(eclipse)
每用一种语言开发,要搭建其编译和开发环境,我们废话不说,立刻来看看Java开发环境的搭建. 1.安装JDK和JRE Windows环境下: a.去Oracle官网下载对应版本的JDK安装包,http: ...
- 系统分区MBR、GPT
分区模式: ①MBR(主引导记录(Master Boot Record))分区:在驱动器最前端的一段引导扇区 缺点:主分区不超过4个,单个分区容量最大2TB 分区工具fdisk只能给硬盘做MBR分区, ...
- JS学习笔记(五) HTML DOM
参考资料: 1. http://www.w3school.com.cn/js/js_htmldom.asp 2. http://www.runoob.com/htmldom/htmldom-tutor ...
- HDU1045 Fire Net(DFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- redis数据库选择-select
1.关于redis的select命令用法: SELECT index 切换到指定的数据库,数据库索引号 index 用数字值指定,以 0 作为起始索引值. 默认使用 0 号数据库. 可用版本: > ...