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

100

10

// 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;}                                 


0 0
原创粉丝点击