HDU 5050 Divided Land(进制转换)
题意 给你两个二进制数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
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.
Each case contains two binary number represents the length L and the width W of given land. (0 < L, W ≤ 21000)
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.
3
10 100
100 110
10010 1100
Case #1: 10
Case #2: 10
Case #3: 110
HDU 5050 Divided Land(进制转换)的更多相关文章
- HDU 2097 Sky数 进制转换
解题报告:这题就用一个进制转换的函数就可以了,不需要转换成相应的进制数,只要求出相应进制的数的各位的和就可以了. #include<cstdio> #include<string&g ...
- Hdu 5050 Divided Land
题目要求就是做求两个二进制数的gcd,如果是用java的话,这题很简单.但也可以用C++做,只能先给自己留下这个坑了,还在研究c++的做法. import java.math.BigInteger; ...
- HDU 2031 进制转换(10进制转R进制)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2031 进制转换 Time Limit: 2000/1000 MS (Java/Others) M ...
- hdu 2031 进制转换(栈思想的使用)
进制转换 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 1877 又一版 A+B(进制转换)
看了http://lovnet.iteye.com/blog/1690276的答案 好巧妙的方法 递归实现十进制向m进制转换 #include "stdio.h" int m; v ...
- 进制转换,杭电0j-2031
进制转换,杭电0j-2031原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=2031 [Problem Description] 输入一个十进制数N,将它 ...
- SQL Server 进制转换函数
一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...
- [No000071]C# 进制转换(二进制、十六进制、十进制互转)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- JS中的进制转换以及作用
js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...
随机推荐
- oracle 之 内存—鞭辟近里(一)
oracle 之 内存—鞭辟近里(一) 今天是2013-07-8日,这几天一直都在寻找一本深入研究oracle内存的书籍,期间一波三折弄的自己神魂颠倒,但是还是被我拿下了,感谢支持我的朋友,这周打算把 ...
- mysql登录报错 ERROR 1045 (28000)
1.现象: [root@localhost ~]# mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for us ...
- 三点半们耐热i哦好家哦i囧囧【
http://pan.baidu.com/share/link?shareid=3011665141&uk=338692646&third=15 http ...
- 两个div在同一行,两个div不换行
方法一: <div style="display:inline"> <div id="div1" style="float:left ...
- 基于VLC的视频播放器
原文:基于VLC的视频播放器 最近在研究视频播放的功能,之前是使用VideoView.在网上看了一下,感觉不是很好,支持的格式比较少,现在网络视频的格式各种各样,感觉用VideoView播放起来局限性 ...
- 使用scipy进行聚类
近期做图像的时候,突然有个idea,须要进行聚类,事实上算法非常easy,可是当时非常急.就直接使用了scipy的cluster. 使用起来事实上非常easy,可是中文的文章非常少,所以就简单的介绍一 ...
- PopupMenu消失(Dismiss)抓住
一.在class在implements PopupMenu该监听器类:OnDismissListener, 然后在类需要实现一个方法未实现:onDismiss(PopupMenu menu). 二.参 ...
- Codeforces Round #256 (Div. 2) C. Painting Fence 或搜索DP
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- jenkins集群加入Windows 2012 server作为slave
必须安装.net framework 3.5, 參考: http://technet.microsoft.com/en-us/library/dn482071.aspx 不要在windows 2012 ...
- C语言程序代写(QQ:928900200)
1.学生成绩统计 要求描述: 用结构数组实现学生信息的统计功能. struct student { long no; /*学号*/ char name[10]; /*姓名*/ char sex; /* ...