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. 自定义刻度的SeekBar

    <com.imibaby.client.views.CustomSeekbar android:id="@+id/myCustomSeekBar" android:layou ...

  2. Monkeyrunner小脚本关于camera的使用

    一下代码涉及自动执行camera,属性一个小设置,恢复初始值,并在中间添加截屏功能 将两个截屏进行前后对比,并返回值 适合初学者,刚刚了解monkeyrunner 的人员来看 注意:一下脚本如果不能执 ...

  3. js+cookie 购物车

    $(function () { //var ctx = new Ch(); //ctx.Clear(); //$.cookie(ctx.cookieName, ""); //ale ...

  4. PHP标准注释

    "php是一门及其容易入门的语言,刚入门的新手不到几分钟的时间可能就会用echo打印出一个hello world !但是他是真正的程序员吗?怎么来定义程序员呢?如果想真正成为一个程序员,那么 ...

  5. Java线程池入门

    序 为什么要用线程池?什么情况下才会用到线程池? 并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间. 因此 ...

  6. 字符串对象-String

    新建字符串对象 ① 直接赋值 ② 构造函数 ③ 转换函数 1    length              字符串对象属性 2    match()  null     跟php中的preg_matc ...

  7. javascript面向对象(1)

    主要内容: 在讲面向对象之前,我们先看一个示例: 类似这种情况大家都能够理解,但是,如果把代码改动一下,请再看一下: 为什么会这样? 如果把代码再改一下看看,如把变量改成b,则直接报错 JavaScr ...

  8. Selenium2+python 常用函数汇总

    1. driver = webdriver.Chrome()  --新建实例 2.driver.find_element_by_id("username") ---通过标签属性id ...

  9. redis命令总结

     Redis命令总结 redis 127.0.0.1:6379> info  #查看server版本内存使用连接等信息 redis 127.0.0.1:6379> client list  ...

  10. log4j向oracle中插入一条系统当前时间的sql语句

    配置log4j,要向oracle插入一条系统当前时间的sql语句,按网上查找的总是出现各种各样的报错,最后总结出的写法是: ### shezhi### log4j.rootLogger = debug ...