Bzoj 1876: [SDOI2009]SUPERGCD (more subtractive + high precision)

Source: Internet
Author: User
Tags greatest common divisor

More subtractive loss, to use high precision ....

---------------------------------------------------------------

#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>using namespace std;const int MAXN = 10009;Char S[MAXN];int POWER[MAXN];struct BIGINT {static const int B = 10000000;static const int W = 7;static const int MAXN = 10000;int n, S[MAXN];BigInt () {n = 0;memset (s, 0, sizeof s);}BOOL Operator < (const BIGINT &t) Const {if (n! = T.N) return n < T.N;for (int i = n; i--;)if (s[i]! = T.s[i]) return s[i] < t.s[i];return true;}bool Operator! = (const BigInt &t) Const {return (*this < T)! = (T < *this);}BigInt Operator-= (const BigInt &t) {for (int i = 0; i < T.N; i++) {if (S[i] < t.s[i])S[i] + = B, s[i + 1]--;S[i]-= t.s[i];}for (int i = T.N; i < n; i++) if (S[i] < 0)S[i] + = B, s[i + 1]--;while (n > 0 &&!s[n-1]) n--;return *this;}void Read () {scanf ("%s", s);int N = strlen (S), p = n; n = n/w;for (int i = 0; i < n; i++) {For (int j = p-w; J < P; j + +)S[i] = s[i] * ten + s[j]-' 0 ';P-= W;}if (N% W) {for (int i = 0; i < P; i++)S[n] = s[n] * ten + s[i]-' 0 ';n++;}}void Write () {for (int i = n; i--;) {if (i! = n-1) {int t = 0;for (int v = s[i]; v; v/=) t++;if (!t) t++;While (T < W)Putchar (' 0 '), t++;}printf ("%d", s[i]);}Putchar (' \ n ');}inlinebool Chk () {return n = = 1 && s[0] = = 1;}inline bool Even () {return! (S[0] & 1);}void Div2 () {for (int i = n; i--;) {if (S[i] & 1)S[i-1] + = B;S[i] >>= 1;}if (!s[n-1]) n--;}void Mul2 () {for (int i = n; i--;) s[i] <<= 1;for (int i = 0; i < n; i++)if (s[i] >= b) s[i]-= B, S[i + 1]++;if (s[n]) n++;}} A, B, *a = &a, *b = &B;int main () {A->read (); B->read ();int cnt = 0;While (*a! = *b) {if (A->chk ()) break;if (A->even ()) {a->div2 ();if (B->even ()) B->div2 (), cnt++;} else if (B->even ()) {b->div2 ();} else {if (*b < *a) Swap (b, a);*b-= *a;}}while (cnt--) a->mul2 ();a->write ();return 0;}

---------------------------------------------------------------

1876: [SDOI2009]SUPERGCD time limit: 4 Sec Memory Limit: MB
Submit: 2277 Solved: 770
[Submit] [Status] [Discuss] Descriptionsheng Bill has an amazing mental arithmetic ability, and can even use the brain to figure out two huge numbers of gcd (greatest common divisor)! So he often contests with others to calculate gcd. One day Sheng Bill very arrogant to find you, and asked to play with you, but lost to Sheng Bill is not very embarrassing! So you decided to write a program to teach him. Input is a total of two lines: the first line: a number A. Second line: a number B. An output row that represents the greatest common divisor of a and B. Sample Input12
54
Sample Output6
HINT

For 20% of data, 0 < A, b≤10 ^ 18.
For 100% of data, 0 < A, b≤10 ^ 10000.

Source

Day1

Bzoj 1876: [SDOI2009]SUPERGCD (more subtractive + high precision)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.