Sicily 1732. Alice and Bob
来源:互联网 发布:单片机时间片轮询 编辑:程序博客网 时间:2024/05/18 15:04
1732. Alice and Bob
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Alice is a beautiful and clever girl. Bob would like to play with Alice.
One day, Alice got a very big rectangle and wanted to divide it into small square pieces. Now comes a problem: if all pieces of small squares are of the same size, how big could the squares be? To Alice, it’s easy to solve the problem. However, she was very busy, so she asked Bob to help her. You know Alice is such a lovely girl and of course Bob won’t refuse her request. But Bob is not so smart and he is especially weak in math. So he turns to you—a genius at programming.
Alice will inform Bob the length and width of the big rectangle, and Bob have to tell her the longest length for the small square. All of these numbers are in their binary representations.
Input
The first line of the input is a positive integer. This is the number of the test cases followed. Each test case contains two integer L and W in their binary representation which tells you the length and width of the very big rectangle (0<L, W<2^1000). There may be one or several spaces between these integers.
Output
The output of the program should consist of one line of output for each test case. The output of each test case only contains the longest length for the small squares in its binary representation. No any redundant spaces are needed.
Sample Input
2100 1000100 110
Sample Output
10010
// Problem#: 1732// Submission#: 3583806// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <string.h>struct number { short n[1050], l; number() { for (int i = 0; i < 1050; i++) n[i] = 0; l = 0; } number(char * a) { int length = strlen(a); l = length; for (int i = length - 1; i >= 0; i--) n[length - 1 - i] = a[i] - '0'; for (int i = length; i < 1050; i++) n[i] = 0; }};int cmp(number & a, number & b) { if (a.l > b.l) return 1; if (a.l < b.l) return -1; for (int i = a.l - 1; i >= 0; i--) { if (a.n[i] > b.n[i]) return 1; else if (a.n[i] < b.n[i]) return -1; } return 0;}void divide2(number & a) { if (a.l == 1) { a.n[0] = 0; a.l = 0; return; } for (int i = 0; i < a.l; i++) a.n[i] = a.n[i + 1]; a.n[--a.l] = 0;}number subtraction(number a, number & b) { number ans; for (int i = 0; i < a.l; i++) { if (a.n[i] >= b.n[i]) { ans.n[i] = a.n[i] - b.n[i]; } else { ans.n[i] = a.n[i] + 2 - b.n[i]; a.n[i + 1] -= 1; } } ans.l = 1049; while (ans.l && ans.n[ans.l - 1] == 0) ans.l--; return ans;}void gcd(number a, number b) { int plus2 = 0; while (a.l && b.l) { if (a.n[0]) { if (b.n[0]) { if (cmp(a, b) == -1) b = subtraction(b, a); else a = subtraction(a, b); } else { divide2(b); } } else { if (b.n[0]) { divide2(a); } else { divide2(a); divide2(b); plus2++; } } } if (b.l) for (int i = b.l - 1; i >= 0; i--) printf("%d", b.n[i]); else for (int i = a.l - 1; i >= 0; i--) printf("%d", a.n[i]); while (plus2--) printf("0"); printf("\n");}int main() { int caseNum; scanf("%d", &caseNum); while (caseNum--) { char a[1005], b[1005]; scanf("%s%s", a, b); number n1(a), n2(b); gcd(n1, n2); } return 0;}
- Sicily 1732. Alice and Bob
- Sicily 1798. Alice and Bob
- sicily--1798. Alice and Bob
- Sicily 1798. Alice and Bob
- Sicily 13289. Alice and Bob
- Sicily 2683. Alice and Bob
- Sicily 1798. Alice and Bob
- Sicily 1732 Alice and Bob (二进制数的最大公约数)
- Alice and Bob
- 2683. Alice and Bob
- 1798. Alice and Bob
- hdu4268 Alice and Bob
- HDU4268 Alice and Bob
- Alice, Bob and Chocolate
- hdu4268 Alice and Bob
- A:Alice and Bob
- hdu4111 Alice and Bob
- Alice and Bob
- 我的总结:java泛型
- Sicily 1733. Berth Allocation
- 黑马程序员-OC内存管理
- 天声人語 20150315
- SGU - 186 - The Chain (贪心)
- Sicily 1732. Alice and Bob
- 日经春秋 20150315
- 佟刚老师《Spring4视频教程》学习笔记(9)(未完成)
- 文章标题
- 日经社説 20150315 許せぬイラクの文化財破壊
- 1019. General Palindromic Number (20)
- 解析网络返回字符串
- Wooden Sticks
- 第十八周项目2:猴子选大王