Modulo C (n, m) % (10 ^ 10)

Source: Internet
Author: User

Address: http://codeforces.com/gym/100110 question B

Http://hi.baidu.com/aekdycoin/item/e051d6616ce60294c5d249d7

Test Data

1515151 1213
.... 0836060000
151144 2002
.... 3558733440
10000000000 11411
.... 0000000000
115123131 1210
.... 2126393000
54546161515130 121231321
.... 6496000000

Because I am too watery in number theory, I spent more than a day on this topic, and finally found it a floating point Precision problem ......

Evaluate C (n, m) % (10 ^ 10), n <= 10 ^ 18 0 <= m <= N

There must be some number theory knowledge in such a large scope ~

Note that 10 ^ 10 can be split into 2 ^ 10*5 ^ 10

Can we modulo and merge the two items separately !!!!

So the current task is to find

C (n, m) % 2 ^ 10;

And C (n, m) % 5 ^ 10;

We can see that C (n, m) % (P ^ A) P is a prime number.

After reading the ultimate version of the AC blog, you will definitely do this.

Example C (n, m) % 1024

Then convert it to n! % 1024

The denominator of the numerator is obtained separately, and then the inverse is obtained.

1*2*3 * .. * 1024

1025 *... * 2048

...

It is obvious that the result is the same for a period of 1024 (cyclical !!!), There are still a few remaining parts, and the brute force is enough.

Note: if we want to obtain the inverse element of the denominator, the denominator must have mutual quality with 2. Therefore, we first remove the multiples of 2. Obviously, the periodicity is still satisfied.

For example, 11 is required now! % (2 ^ 10)

Assume that calc (n, MoD) can calculate n! MoD remainder of all odd-number products in

1*2*3*4*5*6*7*8*9*10*11

1*3*5*7*9*11*2*4*6*8*10 = 1*3*5*7*9*11*2 ^ 5*(1*2*3*4*5 ), the problem can be solved recursively !!!!

Therefore, you can write the formula calc (11/2) * 2 ^ (11/4) * calc (5) * (2 ^ 11/8) * calc (2) * 2 ^) * calc (1)

Complexity is log-level

This question is special. There are only two qualitative factors. When p is a general number, it is better to separate it and combine it with the Chinese Remainder Theorem.

However, if P ^ A is large after prime factor decomposition, the above method cannot be used because the array cannot be opened and the cycle cannot be used ....

So when n m K is a 10 ^ 9 value, it seems that it cannot be done ????

I think K is 10 ^ 9, and m must be 10 ^ 6.

If K is 10 ^ 5, n m can be used at will.

Determine whether C (n, m) is greater than 10 ^ 10. If double is used in the middle, WA 9 is used at the beginning. If it is not checked out, use EPS to determine whether it will pass.

Another way is to refer to the code area as follows:

Paste code

# Include <cstdio> # include <cstring> # include <cmath> typedef long LLD; const LLD mod5 = 9765625; LLD n, m; lld fac [10000000]; LLD two [64]; LLD five [64]; lld pow (lld a, lld B, LLD mod) {LLD ans = 1; while (B) {If (B & 1) ans = ans * A % MOD; A = A * A % MOD; B >>= 1;} return ans;} LLD exgcd (LLD, lld B, LLD & X, LLD & Y) {If (B = 0) {x = 1; y = 0; return ;} else {LLD d = exgcd (B, A % B, x, y); lld t = x; X = y; y = T-A/B * Y; return D ;}} LLD inv Erse (LLD num, LLD mod) {lld x, Y; exgcd (Num, Mod, x, y); While (x <0) x + = mod, y-= num; return X;} void init1 () {two [0] = 1; for (LLD I = 1; I <64; I ++) two [I] = two [I-1] * 2; FAC [1] = 1; FAC [0] = 1; for (LLD I = 2; I <= 1024; I ++) {LLD num = I; FAC [I] = FAC [I-1]; if (I & 1) FAC [I] = FAC [I] * num % 1024;} LLD calc (LLD N, LLD mod) // n! Remove the multiples of 2 or 5 and then return the remainder to mod {lld t = N/MOD; LLD ans = 1; if (T> = 1) ans = POW (FAC [mod], t, MoD); int Lim = n % MOD; return ans * FAC [Lim] % MOD;} LLD Gao (lld B [], LLD mod, LLD num) // calculate C (n, m) % mod {long fenzi _ = 0, fenmu _ = 0; LLD Tn = N, TM = m, = N-m; for (LLD I = 1; B [I] <= tn; fenzi _ + = (TN/B [I]), I ++); For (LLD I = 1; B [I] <= TM; fenmu _ + = (TM/B [I]), I ++); For (LLD I = 1; B [I] <=; fenmu _ + = (/B [I]), I ++); LLD CNT = fenzi _-fenmu _; LLD F Enzi = calc (TN, MoD); For (LLD I = 1; B [I] <= tn; I ++) fenzi = fenzi * calc (TN/B [I], MoD) % MOD; LLD fenmu = calc (DMA, MoD); For (LLD I = 1; B [I] <=; I ++) fenmu = fenmu * calc (/B [I], MoD) % MOD; fenmu = fenmu * calc (TM, MoD) % MOD; For (LLD I = 1; B [I] <= TM; I ++) fenmu = fenmu * calc (TM/B [I], MoD) % MOD; return fenzi * inverse (fenmu, MoD) % mod * POW (Num, CNT, MoD) % MOD;} void init2 () {five [0] = 1; for (LLD I = 1; I <= 50; I ++) Five [I] = five [I-1] * 5; FAC [1] = 1; FAC [0] = 1; for (LLD I = 2; I <= mod5; I ++) {LLD num = I; FAC [I] = FAC [I-1]; if (I % 5! = 0) FAC [I] = FAC [I] * num % mod5 ;}// X % 1024 = A; // X % mod5 = B; // The above two formulas merge lld crt (lld a, lld B) {LLD mod = limit 00ll; LLD X, Y; exgcd (mod5, 1024, X, y); X = x * A % MOD; lld p = x, Q; exgcd (1024, mod5, x, y); X = x * B % MOD; Q = x; LLD ans = (mod5 * P % mod + 1024ll * q % mod) % MOD; Return (ANS + mod) % MOD;} void print (LLD ans) {If (n-M <m) M = N-m; Double T = 1; lld a = 1; bool flag = false; For (LLD I = 1; I <= m; I ++) {T = T * (N-I + 1)/I; A = A * (N-I + 1)/I; if (a >= 10000000000ll | T >=1e10) {flag = true; break ;}} if (FLAG) printf ("... % 010i64d \ n ", ANS); else printf (" % i64d \ n ", ANS);} int main () {freopen (" combi. in "," r ", stdin); freopen (" combi. out "," W ", stdout); While (scanf (" % i64d % i64d ", & N, & M )! = EOF) {init1 (); lld a = Gao (two, 1024,2); init2 (); lld B = Gao (five, mod5, 5 ); LLD ans = CRT (a, B); print (ANS);} return 0;}/* 1515151 1213... 0836060000151144 2002... 355873344010000000000 11411... 0000000000115123131 1210... 212639300054546161515130 121231321... 6496000000 */

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.