题意  给你两个二进制数m,n   求他们的最大公约数  用二进制表示  0<m,n<2^1000

先把二进制转换为十进制  求出最大公约数  再把结果转换为二进制  数比較大要用到大数

import java.util.*;
import java.math.*; public class wl6_9 { static BigInteger two = BigInteger.valueOf(2), one = BigInteger.ONE,
zero = BigInteger.ZERO; static BigInteger gcd(BigInteger a, BigInteger b) {
while (!(a.mod(b).equals(zero))) {
BigInteger t = a.mod(b);
a = b;
b = t;
}
return b;
} static void bprint(BigInteger x) {
if (x.equals(zero))
return;
bprint(x.divide(two));
if (x.mod(two).equals(one))
System.out.print(1);
else
System.out.print(0);
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
String a, b;
for (int t = 1; t <= T; t++) {
a = in.next();
b = in.next();
int la = a.length(), lb = b.length(); BigInteger m = zero, n = zero;
for (int i = 0; i < la; ++i) {
m = m.multiply(two);
if (a.charAt(i) == '1')
m = m.add(one);
} for (int i = 0; i < lb; ++i) {
n = n.multiply(two);
if (b.charAt(i) == '1')
n = n.add(one);
}
System.out.printf("Case #%d: ", t);
bprint(gcd(m, n));
System.out.println();
}
in.close();
}
}

Divided Land

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
 
Source
 

HDU 5050 Divided Land(进制转换)的更多相关文章

  1. HDU 2097 Sky数 进制转换

    解题报告:这题就用一个进制转换的函数就可以了,不需要转换成相应的进制数,只要求出相应进制的数的各位的和就可以了. #include<cstdio> #include<string&g ...

  2. Hdu 5050 Divided Land

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

  3. HDU 2031 进制转换(10进制转R进制)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2031 进制转换 Time Limit: 2000/1000 MS (Java/Others)    M ...

  4. hdu 2031 进制转换(栈思想的使用)

    进制转换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. HDU 1877 又一版 A+B(进制转换)

    看了http://lovnet.iteye.com/blog/1690276的答案 好巧妙的方法 递归实现十进制向m进制转换 #include "stdio.h" int m; v ...

  6. 进制转换,杭电0j-2031

    进制转换,杭电0j-2031原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=2031 [Problem Description] 输入一个十进制数N,将它 ...

  7. SQL Server 进制转换函数

    一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...

  8. [No000071]C# 进制转换(二进制、十六进制、十进制互转)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. JS中的进制转换以及作用

    js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...

随机推荐

  1. VSTO学习笔记(九)浅谈Excel内容比较

    原文:VSTO学习笔记(九)浅谈Excel内容比较 说起文件内容比较,或许我们首先想到的是UltraCompare这类专业比较的软件,其功能非常强大,能够对基于文本的文件内容作出快速.准确的比较,有详 ...

  2. 【WinRT】【译】【加工】在 XAML 中制作圆形图片

    原文:[WinRT][译][加工]在 XAML 中制作圆形图片 原文地址:http://timheuer.com/blog/archive/2015/05/06/making-circular-ima ...

  3. Android开发之使用URL訪问网络资源

    Android开发之使用URL訪问网络资源 URL (UniformResource Locator)对象代表统一资源定位器,它是指向互联网"资源"的指针. 资源能够是简单的文件或 ...

  4. 向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题

    原文:向GridView的模板列绑定OnClientClick的函数时出现了奇怪的问题 GridView的一个模板列中的内容是按钮,需要实现以下的效果: GridView分页显示数据,点击编辑按钮(模 ...

  5. ArrayBlockingQueue和LinkedBlockingQueue的区别

    ArrayBlockingQueue和LinkedBlockingQueue的区别,得出结论如下: 1. 队列中锁的实现不同 ArrayBlockingQueue实现的队列中的锁是没有分离的,即生产和 ...

  6. hdu4405(概率dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:跳棋有0~n个格子,每个格子X可以摇一次色子,色子有六面p(1=<p<=6), ...

  7. c语言输入输出函数

    上学年学习c语言的时候比较匆忙,没好好吸收. 现在有时间好好复习下. 本文就c语言常见输入函数进行简单介绍,对比. ps:由于自己能力有限,时间有限,多数介绍,总结都是摘录网上相关学习资料,下面给出本 ...

  8. Linux进程同步之记录锁(fcntl)

    记录锁相当于线程同步中读写锁的一种扩展类型,可以用来对有亲缘或无亲缘关系的进程进行文件读与写的同步,通过fcntl函数来执行上锁操作.尽管读写锁也可以通过在共享内存区来进行进程的同步,但是fcntl记 ...

  9. hdu 4465 Candy(2012 ACM-ICPC 成都现场赛)

    简单概率题,可以直接由剩余n个递推到剩余0个.现在考虑剩余x个概率为(1-p)的candy时,概率为C(2 * n - x, x) * pow(p, n + 1)  *pow(1 - p, n - x ...

  10. 蚁群算法 matlab程序(已执行)

    下面是解放军信息project大学一个老师编的matlab程序,请尊重原作者劳动,引用时请注明出处. 我经过改动添加了凝视,已经执行过,无误, function [R_best,L_best,L_av ...