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. nerual style 执行命令

    python neural_style.py --content ./examples/4-content.jpg --styles ./examples/4-faguo-style.jpg --ou ...

  2. html5 录制mp3音频,支持采样率和比特率设置

    13年的时候做过html5录音,一个问题是保存的wav格式文件很大,当初用了一个迂回的方式,上传到服务器后调用 lame 编码器转换,但由于文件大,上传较慢.不得不说,前端技术发展真是日新月异,有人实 ...

  3. 【转】三十分钟掌握STL

    转自http://net.pku.edu.cn/~yhf/UsingSTL.htm 三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以 ...

  4. My97Datepicker 去掉 “不合法格式或超期范围”自动纠错限制

    官网上,纠错有以下三种,如日期格式不对,或超期,则必须纠错过后,才能继续操作, 但有时,可能允许出错,需要把纠错功能去掉,则可以设置errDealMode = 3,这种模式是官网说没有的, 但能够去掉 ...

  5. cassandra安装

    从官网下载下来的包解压后有100多M,里面包含了已经编译好的全部程序. 按照方法,进入目录后运行 bin/cassandra -f 运行不成功. 然后根据"https://wiki.apac ...

  6. 如何让电脑公司Win7系统自动关闭停止响应的程序

    在注册表编辑器窗口左侧,依次展开HKEY_CURRENT_USER\ControlPanel\Desktop,选中Desktop,在右边的窗口中选择AutoEndTasks,双击打开AutoEndTa ...

  7. ios页面弹出方式《笔记》

    1.presentViewController 方式,动画效果是从底部弹出,主要用在除导航类页面的弹出 let anotherVC = UIStoryboard(name: "Main&qu ...

  8. 用Backbone.js教程系列的链接

    整理了一下用Backbone.js系列教程链接. Backbone.js入门教程 用Backbone.js创建一个联系人管理系统(一) 用Backbone.js创建一个联系人管理系统(二) 用Back ...

  9. checkbox标签已有checked=checked属性但是不显示勾选

    点击全选按钮,选中下面的列表,再次点击取消选择. 第一次的使用的方法是$("input[name=xxx]").attr('checked',true); 但是往往刷新页面第一次点 ...

  10. Tomcat的优化

    Tomcat的优化配置 修改tomcat的config目录下server.xml文件 <Connector port="9021"  protocol="HTTP/ ...