Professor Zhou's Big Data Template [full version]

Source: Internet
Author: User
/* This was written by Professor Zhou shangchao many years ago. I hope to read this article and remember that such a mathematician has been quietly contributing !! Let's give a brief introduction to our professors. during the Cultural Revolution, we learned foreign languages, mastered English, met Japanese, Russian, German, and French Foreign Languages, and early excellent intellectuals, A young man who has not yet graduated from Wuhan University on his own in 1983 and is engaged in mathematics after graduation. He has published various academic articles in a number of foreign academic newspapers and has made great achievements in his studies, with the honor of an International mathematician, we have brought out a large number of outstanding talents .... I was lucky to have dinner at the professor's house last time. Thank you very much... today is Christmas. I hope the professor's health will be better... it is also a kind of happiness for students who can become professors in their lives... * // * high-precision computing template function 2011-9-5 file name bigint. the H integer is greater than 2 ^ 64-1. It is called a big integer and must be computed with high precision. For example, if a = 123444455556666777788889999 is a 27-digit integer, it is represented by an array. The minimum 4 digits are represented by a [1] = 9999, A [1] is the single digit of the 10000 hexadecimal number. A [2] = 8888; A [3] = 7777, a [4] = 6666, a [5] = 5555, a [6] = 4444, A [7] = 123; A [0] = 7; indicates that it is the 7-digit number of the hexadecimal number. Base = 10000 hexadecimal. Large integers are read in strings, and then converted into Arrays Using the tran function. The following functions are available in this file: Trans, comp, Pn, copy, add, sub, mult, mult, Div, Div, when high-precision computing is used in the GCD Program, (1) copy this file to the front of int main (), and delete unnecessary functions. (2) You can also add # include <bigint. h> for example, hdu1002 A + B Problem II. You can only use addition to delete sub, mult, mult, and Div. You can use hdu1002 A + B problem iihdu1502 regular wordshdu1042 N for high-precision calculation! N <= inherit hdu1133 buy the ticket http://acm.hdu.edu.cn/showproblem.php? PID = 1133zju2061 http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 2061hdu1134 game of connections catalan number H [1] = 1, h [N] = H [n-1] * (4n-2)/(n + 1 ), n <= 100 hdu1250 hat's Fibonacci http://acm.hdu.edu.cn/showproblem.php? PID = 1250pku2084 game of connections = hdu1134pku2515 birthday cake calculation 1 + 2 ^ m +... + N ^ m, n <= 10 ^ 41, m <= 100pku1220 number base conversionpku2680 computer hour dollar dayzpku3199 uncle jackpku3324 Lucas-Lehmer testpku3331 the idiot Of The Year contest */# include <vector> # include <list> # include <map> # include <set> # include <deque> # include <stack> # include <bitset> # include <algorithm> # include <Functional> # include <numeric> # include <utility> # include <sstream> # include <iostream> # include <iomanip> # include <cstdio> # include <cmath> # include <cstdlib> # include <ctime> # include <stdio. h> # include <string. h> using namespace STD; const int base = 10000; // The maximum value of a hichina Int. If you only use +,-, you can increase the value of base = 000000000int one [] = {1, 1 }; // large integer 1int zero [] = {1, 0}; // large integer 0int comp (int * a, int * B ); // compare the large integer int comp (int * a, int * B, int T); // Large integer compare void trans (char * Ch, int * A); // string-> Number base = 10000, a [1] is a percentile, A [0] is the number of digits (segment number) void Pn (int * A); // printf large integer avoid copy (int * a, int * B ); // A = bvoid add (int * a, int * B, int * C); // large integer + large integer c = a + bvoid sub (int *, int * B, int * C); // big integer a-big integer B, A> bvoid mult (int * a, int B, int * D ); // big integer * integer c = A * bvoid mult (int * a, int * B, int * C ); // big integer * big integer c = A * bint Div (int * a, int B, int * C ); // C = A/B k = A % bvoid Div (int * a, int * B, int * C, Int * r); // C = A/B r = A % bvoid gcd (int * a, int * B, int * D); // d = gcd (, b)/* string --> Number base = 10000, a [1] is the second bit, And a [0] is the number of digits (segment number) the string is 12355558888 --> A [1] = 8888, A [2] = 5555, a [3] = 123 A [0] = 3, each segment seg = 4 digits */void trans (char * Ch, int *) {int L, I, J, K, S, I9, SEG = 4; // seg = 6; // base = 1000000 L = strlen (CH ); S = L-seg; k = 1; for (I = s; I> = 0; I-= seg, K ++) {I9 = I + seg; A [k] = CH [I]-'0'; For (j = I + 1; j <I9; j ++) A [k] = A [k] * 10 + CH [J]-48;} I + = seg; A [k] = 0; For (j = 0; j <I; j ++) A [k] = A [k] * 10 + CH [J]-48; if (a [k]) A [0] = K; else a [0] = K-1;} // large integer compare the size of int comp (int * a, int * B) {int I; if (A [0]> B [0]) return 1; if (a [0] <B [0]) Return-1; for (I = A [0]; i> = 1; I --) {if (a [I]> B [I]) return 1; if (a [I] <B [I]) return-1;} return 0;} int comp (int * a, int * B, int t) {int I, j = 1; for (I = B [0]; i> = 1; I --, J ++) {if (a [T]> B [I]) return J; if (a [T] <B [I]) return-1; t --;} return 0;} void PN (Int * A) {int I; // output "% 04d" 10000 hexadecimal printf ("% d", a [A [0]); for (I = A [0]-1; I> = 1; I --) printf ("% 04d", a [I]); printf ("\ n");} void copy (int * a, int * B) {int I; // A = B for (I = 0; I <= B [0]; ++ I) A [I] = B [I];} // large integer + large integer c = a + B, base hexadecimal, base = 10000 // C [0] indicates the number of segments, and C [1] indicates the percentile bit. // Example 9912348888, C [0] = 3, C [1] = 8888, C [2] = 1234, C [3] = 99 void add (int *, int * B, int * c) {int S, I, T, P, d [1000]; If (B [0] = 1) & (B [1] = 0) {copy (C, A); return;} If (A [0] = 1) & (A [1] = 0) {copy (C, B); return;} if (a [0]> = B [0]) {copy (C, A); copy (D, B) ;}else {copy (C, B); copy (D, a) ;}s = C [0]; T = d [0]; // C [0]> = d [0] C [S + 1] = 0; for (I = 1; I <= T; I ++) {C [I] + = d [I]; If (C [I]> = base) {C [I]-= base; c [I + 1] ++ ;}}for (; I <= s; I ++) {If (C [I]> = Base) {c [I]-= base; C [I + 1] ++;} else break;} If (C [S + 1]> 0) c [0] = S + 1; // process the last digit} // large integer a-big integer B, A> B, C = A-bvoid sub (int * a, int * B, int * c) {int I, j, P = 0; if (B [0] = 1 & (B [1] = 0) {copy (C, A); return;} If (COMP (A, B) <= 0) {copy (C, zero); return;} If (COMP (A, zero) = 0) {copy (C, zero); return ;} for (I = 1; I <= B [0]; I ++) {C [I] = A [I]-B [I]-P; if (C [I] <0) {C [I] + = base; P = 1;} else p = 0;} For (; I <= A [0]; I ++) {C [I] = A [I]-P; If (C [I] <0) {C [I] + = base; P = 1 ;} else p = 0 ;}for (I = A [0]; I> = 1; I --) {If (C [I]) {C [0] = I; break ;}}// big integer a multiply the constant B <base, D = A * bvoid mult (int * a, int B, int * D) {int W, I, p; If (B = 1) {copy (D, a); return;} If (B = 0) | (A [0] = 1 & A [1] = 0) {d [0] = 1; d [1] = 0; return ;} W = a [0]; P = 0; // for (I = 1; I <= W; I ++) {d [I] = A [I] * B + P; If (d [I]> = base) {P = d [I]/base; d [I] % = base;} else p = 0;} If (p) {W ++; d [w] = P;} d [0] = W ;} // big integer a * big integer B, C = A * bvoid mult (int * a, int * B, int * c) {int I, j, S, m, n, K, P; // base <= 10000 m = A [0]; n = B [0]; k = m + n-1; /* m digits * n digits = m + n-1 digits or m + n digits instance 12345678*123456789012 = 1524157764056090136 a [0] = 2 A [1] = 5678; A [2] = 1234; B [0] = 3 B [1] = 9012; B [2] = 5678; B [3] = 1234; */P = 0; for (I = 0; I <= K; I ++) C [I] = 0; C [I] = 0; // A [1], A [2]..., A [m] // B [1] // C [1], C [2],..., c [m] For (j = 1; j <= N; j ++) {S = J; If (B [J]) for (I = 1; I <= m; I ++, s ++) {C [s] + = A [I] * B [J]; If (C [s]> = Base) {c [S + 1] + = C [s]/base; C [s] % = base ;}}for (I = 1; I <= K; I ++) if (C [I]> = base) {C [I + 1] + = C [I]/base; C [I] % = base ;} if (C [k + 1]) K ++; C [0] = K;} // C = A/B k = A % bint Div (int *, int B, int * c) {int I, j, S, T; int K = 0, Q; // long K = 0, Q; base >=100000 if (COMP (A, zero) = 0) {copy (C, zero); Return 0 ;}for (I = A [0]; i> = 1; I --) {q = K * base + A [I]; C [I] = Q/B; k = Q % B ;} if (C [A [0] = 0) {C [0] = A [0]-1;} else C [0] = A [0]; return K;} // big integer except Big integer c = A/B, r = A % bvoid Div (int * a, int * B, int * C, int * r) {int M = A [0], n = B [0], I, J, K, S, T, P, Q, X, Y; int base1000 = base * 1000, base100 = base * 100, base10 = base * 10; // M digits except n digits = k = m-n + 1 digits if (COMP (a, B) <0) {copy (R, a); copy (C, zero); return;} k = m-n + 1; for (S = K; S> = 0; s --) C [s] = 0; for (S = K, t = m; S> = 1, t> = N;) {P = comp (a, B, T ); // A [T]-B [N] // If (p <0 & t = N) break; If (P = 0) {C [s] ++; For (j = T; j> = T-n + 1; j --) A [J] = 0; t-= N; If (T <n) break; s-= N;} If (p> 1) {C [s] ++; for (j = t, q = N; j> = T-n + 1; j --, Q --) A [J]-= B [Q]; for (j = T-n + 1; j <= T; j ++) {if (a [J] <0) {A [J] + = base; A [J + 1] --;} //} For (j = T; j> = T-n + 1; j --) if (a [J]) break; If (j <n) break; s-= T-J; T = J;} If (p <0 & T> N) {A [T-1] + = A [T] * base; A [T] = 0; t --; S --;} If (P = 1) // 9999 1234/1999 9999 x = 4 {x = A [T]/(B [N] + 1 ); c [s] + = x; // x <4999 for (j = t, q = N; q> = 1; q --, j --) A [J]-= x * B [Q]; for (j = T-n + 1; j <= T; j ++) {While (A [J] <=-base1000) {A [J] + = base1000; A [J + 1]-= 1000 ;} while (A [J] <=-base100) {A [J] + = base100; A [J + 1]-= 100 ;} while (A [J] <=-base10) {A [J] + = base10; A [J + 1]-= 10;} while (A [J] <0) {A [J] + = base; A [J + 1] -- ;}}} if (C [k] = 0) C [0] = K-1; else C [0] = K; for (I = m; I> = 1; I --) if (a [I]) break; if (I = 0) copy (R, zero); else {R [0] = I; while (I >= 1) {R [I] = A [I]; I --;}}} // d = gcd (a, B) void gcd (int * a, int * B, int * D) {int P, X [10000], Y [10000], c [10000]; P = comp (a, B); If (p <0) {copy (X, B); copy (Y, );} else {copy (X, A); copy (Y, B);} // x> = B if (COMP (Y, zero) = 0) {copy (D, x); return;} copy (D, Y); While (1) {Div (x, D, C, Y); // C = x/d; y = x % d if (COMP (Y, zero) = 0) {return;} If (COMP (Y, one) = 0) {copy (D, one); return;} copy (x, D); copy (D, Y );}}

 

 

Related Article

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.