Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比
赛计算GCD。有一天Sheng bill很嚣张地找到了你,并要求和你比 赛,但是输给Sheng bill岂不是很丢脸!所以你
决定写一个程序来教训他。
标签:bbs span input geo discuss 大脑 for nbsp get
一行,表示A和B的最大公约数。
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<map> using namespace std; const int mod = 100000000; int tot; struct node { int x[2010], len; }a,b; void read(node &c) { char s[10010]; scanf("%s", s); int len = strlen(s),cnt = 0,chengji = 1; for (int i = len - 1; i >= 0; i--) { cnt = cnt + (s[i] - ‘0‘)* chengji; chengji *= 10; if ((len - i) % 8 == 0) { c.x[++c.len] = cnt; cnt = 0; chengji = 1; } } if (cnt != 0) c.x[++c.len] = cnt; } void div1() { for (int i = a.len; i >= 1; i--) { if (a.x[i] % 2 == 1) a.x[i - 1] += mod; a.x[i] >>= 1; } while (a.x[a.len] == 0 && a.len > 1) a.len--; } void div2() { for (int i = b.len; i >= 1; i--) { if (b.x[i] % 2 == 1) b.x[i - 1] += mod; b.x[i] >>= 1; } while (b.x[b.len] == 0 && b.len > 1) b.len--; } bool check() { if (a.len != b.len) return false; for (int i = 1; i <= a.len; i++) if (a.x[i] != b.x[i]) return false; return true; } bool cmp() { if (a.len > b.len) return true; else if (a.len < b.len) return false; for (int i = a.len; i >= 1; i--) { if (a.x[i] > b.x[i]) return true; else if (a.x[i] < b.x[i]) return false; } return true; } void jian(node &c, node d) { for (int i = 1; i <= c.len; i++) { if (c.x[i] >= d.x[i]) c.x[i] -= d.x[i]; else { c.x[i] = c.x[i] + mod - d.x[i]; c.x[i + 1]--; } } while (c.x[c.len] == 0 && c.len > 1) c.len--; } void mul() { for (int i = 1; i <= a.len; i++) a.x[i] <<= 1; for (int i = 1; i <= a.len; i++) if (a.x[i] >= mod) { a.x[i] -= mod; a.x[i + 1]++; } while (a.x[a.len + 1]) a.len++; } void print() { printf("%d", a.x[a.len]); for (int i = a.len - 1; i >= 1; i--) printf("%08d", a.x[i]); printf("\n"); } int main() { read(a); read(b); while (a.x[1] % 2 == 0 && b.x[1] % 2 == 0) { tot++; div1(); div2(); } while (!check()) { if (cmp()) jian(a, b); else jian(b, a); while (a.x[1] % 2 == 0) div1(); while (b.x[1] % 2 == 0) div2(); } while (tot--) mul(); print(); return 0; }
标签:bbs span input geo discuss 大脑 for nbsp get
原文地址:http://www.cnblogs.com/zbtrs/p/7397038.html