Divided Land

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 123    Accepted Submission(s): 64

Problem Description
It’s time to fight the local despots and redistribute the land. There
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.
 
Input
The first line of the input is T (1 ≤ T ≤ 100), which stands for the number of test cases you need to solve.

Each case contains two binary number represents the length L and the width W of given land. (0 < L, W ≤ 21000)

 
Output
For each test case, print a line “Case #t: ”(without quotes, t means
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.
 
Sample Input
3
10 100
100 110
10010 1100
 
Sample Output
Case #1: 10
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(二进制求最大公约数)的更多相关文章

  1. HDU 5050 Divided Land(进制转换)

    题意  给你两个二进制数m,n   求他们的最大公约数  用二进制表示  0<m,n<2^1000 先把二进制转换为十进制  求出最大公约数  再把结果转换为二进制  数比較大要用到大数 ...

  2. Hdu 5050 Divided Land

    题目要求就是做求两个二进制数的gcd,如果是用java的话,这题很简单.但也可以用C++做,只能先给自己留下这个坑了,还在研究c++的做法. import java.math.BigInteger; ...

  3. 二进制求最大公约数&&输出二进制

    Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  4. HUD 5050 Divided Land

    http://acm.hdu.edu.cn/showproblem.php?pid=5050 题目大意: 给定一个矩形的长和宽,把这个矩形分成若干相等的正方形,没有剩余.求正方形的边长最长是多少. 解 ...

  5. HDU - 5050 (大数二进制gcd)

    It's time to fight the local despots and redistribute the land. There is a rectangular piece of land ...

  6. 一个好的函数(gcd)求最小公约数

    这个函数是我无意中看到的很不错,很给力,我喜欢 是用于求最小公约数的 简单的描述就是,记gcd(a,b)表示非负整数a,b的最大公因数,那么:gcd(a,b)=gcd(b,a%b)或者gcd(a,0) ...

  7. HDU 2503 a/b + c/d(最大公约数与最小公倍数,板子题)

    话不多说,日常一水题,水水更健康!┗|`O′|┛ 嗷~~ a/b + c/d Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768 ...

  8. Euclid求最大公约数

    Euclid求最大公约数算法 #include <stdio.h> int gcd(int x,int y){ while(x!=y){ if(x>y) x=x-y; else y= ...

  9. 算法:欧几里得求最大公约数(python版)

    #欧几里得求最大公约数 #!/usr/bin/env python #coding -*- utf:8 -*- #iteration def gcd(a,b): if b==0: return a e ...

随机推荐

  1. RabbitMQ在CentOS上的简单安装配置

    安装 1.依赖Erlang,yum install erlang安装之 2.去官网下载Fedora/RHEL的rpm包,rpm -ivh rabbitmq-server-*.noarch.rpm 安装 ...

  2. 修改数据库表的schema,(表的[dbo.]前缀)

    数据库使用过程中遇到这种问题,请看下图

  3. CentOS最小化安装后,增加GNOME桌面

    背景:下载CentOS 7的安装包后,在虚拟机上安装. 上来就遇到一个问题:提示需要开启intel vt-x. 这个进入BIOS,在CPU的设置中开启即可. 然后怀着兴奋的心情,开始各种下一步的安装, ...

  4. Smart Forms&ScriptFrom

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. servlet&jsp高级:第二部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. hdu 3033 I love sneakers! 分组背包

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. select into from 和 insert into select 的区别和用法及 SQL SELECT INTO 中Undeclared variable错误解决办法

    今天试了一下数据表中的数据备份到另一个空的数据表,然后使用了SQL SELECT INTO语句,然后提示Undeclared variable......错误,现在在这里做下总结并给出解决办法. 应用 ...

  8. 2013 Multi-University Training Contest 8

    HDU-4676 Sum Of Gcd 题意:给定一个1-N的全排列序列,N<=20000,有Q组询问,Q<=20000,每组询问给出左右区间[l, r],问区间内的任意两个数的gcd之和 ...

  9. 阿里云大数据三次技术突围:Greenplum、Hadoop和“飞天”

    阿里云大数据三次技术突围:Greenplum.Hadoop和"飞天"    对于企业来说,到底什么是云计算?相信很多企业都有这样的困惑,让我们一起回到这个原始的起点探讨究竟什么是云 ...

  10. eclispse 中集成多个tomcat

    1.背景 在本地需要运行两个项目进行测试时,需要同时启动两个服务器,所以集成多个Tomcat到eclipse就成为一个必要的知识点. 2.准备知识 2.1 因为同时在一台主机上运行,所以多个服务器共用 ...