Divided Land

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 56    Accepted Submission(s): 27 
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)
 
OutputFor 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
 
Source
 

#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);
int num;
for(num=;num<=cases;num++)
{
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: ",num);
gcd(n1,n2);
}
return ;
}

二进制求最大公约数&&输出二进制的更多相关文章

  1. hdu----(5050)Divided Land(二进制求最大公约数)

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

  2. 把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列。

    编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列. 实现了unsigned long型的转换. // 十进制转换为二进制,十进制数的每1bit转换为二进制的1 ...

  3. java语言将任意一个十进制数数字转换为二进制形式,并输出转换后的结果

    package com.llh.demo; import java.util.Scanner; /** * * @author llh * */ public class Test { /* * 将任 ...

  4. C语言 · 求最大公约数

    算法提高 求最大公约数   时间限制:1.0s   内存限制:512.0MB      编写一函数gcd,求两个正整数的最大公约数. 样例输入: 5 15样例输出:5 样例输入: 7 2样例输出:1 ...

  5. 递归--练习3--noi7592求最大公约数问题

    递归--练习3--noi7592求最大公约数问题 一.心得 两个低级错误:1. ll setMax(ll &m,ll &n)中无引用,结果只传值,没传地址2. return f(n,m ...

  6. 求最大公约数和最小公倍数_python

    """写两个函数,分别求两个整数的最大公约数和最小公倍数,调用这两个函数,并输出结果.两个整数由键盘输入.""" ''' 设两个整数u和v, ...

  7. 用gcd库函数求最大公约数

    如何直接调用库函数来求最大公约数呢? 1.首先看怎样求两个数的最大公约数 要注意gcd()前面是两个“_” !!! #include<bits/stdc++.h> using namesp ...

  8. 算法:辗转相除法求最大公约数(C语言实现)

    辗转相除法,一种求最大公约数的算法 已知:A / B = C ······ R  (A.B.C.R皆是整数) 假设:D是A的余数,D也是B的余数,那么D就是A和B的公约数 D是A和B的约数,则A和B是 ...

  9. [算法]求满足要求的进制(辗转相除(欧几里得算法),求最大公约数gcd)

    题目 3在十进制下满足若各位和能被3整除,则该数能被3整除. 5在十六进制下也满足此规律. 给定数字k,求多少进制(1e18进制范围内)下能满足此规律,找出一个即可,无则输出-1. 题解 写写画画能找 ...

随机推荐

  1. (转)oracle中用户删除不了,ORA-01940提示 “无法删除当前已连接用户”

    Oracle删除用户的提示无法删除当前已连接用户两种解决方法如下: 1.先锁定用户.然后查询进程号,最后删除对应的进程.在删除对应的用户 SQLalter user XXX account lock; ...

  2. SPFA+寻路(行路难,洛谷2832)

    啊啊啊这道难题总算是做出来了,首先是帅比浮云的题解发出来一下:http://www.cnblogs.com/fuyun-boy/p/5922742.html 原题目地址:https://www.luo ...

  3. NC nc5.x报表设置合计行是否显示

    首先要先继承UI类 /** * 设置合计行是否显示 */ public TotalsReportUI() { super(); getReportBase().getBodyPanel().setTo ...

  4. Shell文本处理 - 分割合并与过滤

    sort分类操作 示例文件 Boys in Company C:HK:192:2192 Alien:HK:119:1982 The Hill:KL:63:2972 Aliens:HK:532:4892 ...

  5. 使用 VisualCode + iTerm2 提交github的Pull Request

    VisualCode集成github功能,是程序猿参与开源项目的利器.相比Sublime简单了很多(插件安装繁琐,比如你试试在Sublime2 安装gosublime,这里有坑; Sublime 3修 ...

  6. 数据库优化系列——SQL性能优化十条建议

    1.查询的模糊匹配  尽量避免在一个复杂查询里面使用 LIKE '%parm1%'—— 红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 解决办法: 其实只需要对该脚本略做改进,查询速度便 ...

  7. setAutoCommmit保持数据的完整性

    setAutoCommit总的来说就是保持数据的完整性,一个系统的更新操作可能要涉及多张表,需多个SQL语句进行操作 循环里连续的进行插入操作,如果你在开始时设置了:conn.setAutoCommi ...

  8. BOOTSTRAP定制

    1.补充:栅格系统中调整列的位置/顺序 (1)方法1:偏移量(col-*-offset-*) (2)方法2:对列进行push/pull操作 col-lg-pull-1        ~         ...

  9. Maven插件之maven-archetype-plugin

    Maven插件之maven-archetype-plugin 时间:2014-03-29 学过Maven的人,都知道用MyEclipse的Maven插件生成一个项目骨架,比如maven-archety ...

  10. css js 的引入方式和书写位置

    css 的引入方式 1.行内样式 <div id="div1" style="width:100px; height:100px; background:red&q ...