The main idea: the combination of the number of modulo, N and M is not large, p is large and composite.
Idea: Violent decomposition + fast power
Note: Violence is also different, decomposition factorization can use the following work function, written very ingenious, excerpt from the Internet.
1#include <iostream>2#include <cstring>3 using namespacestd;4 5typedefLong Longll;6 Constll mod = 1ll << +;7 Const intN =1000001;8 Const intM =100007;9 BOOLIs_prime[n];Ten intPrime[m]; One intp; A - void Get() - { thememset (Is_prime,true,sizeof(Is_prime)); -is_prime[0] = is_prime[1] =false; - for(inti =2; i < N; i++ ) - { + if(Is_prime[i]) - { + intj = i *i; A if(J >= N) Break; at Do - { -IS_PRIME[J] =false; -J + =i; -} while(J <N); - } in } -p =0; to for(inti =0; i < N; i++ ) + { - if(Is_prime[i]) the { *prime[p++] =i; $ }Panax Notoginseng } - } the + ll Pow_mod (ll A, ll N) A { thell ans =1; +A = a%MoD; - while(n) $ { $ if(N &1 ) - { -Ans = ans * a%MoD; the } -n = n >>1;WuyiA = a * a%MoD; the } - returnans; Wu } - About ll Work (ll N, ll Q) $ { -ll ans =0; - while(n) - { AAns + = n/Q; +N/=Q; the } - returnans; $ } the the ll C (ll N, ll M) the { thell ans =1; - for(inti =0; I < P && Prime[i] <= N; i++ ) in { thell x =Work (n, Prime[i]); thell y = Work (n-m, Prime[i]); Aboutll z =Work (M, Prime[i]); thex = x-(y +z); theAns = ans * pow_mod (Prime[i], x)%MoD; the } + returnans; - } the Bayi intMain () the { the Get(); - intT; -CIN >>T; the while(t-- ) the { the ll N, m; theCIN >> N >>m; -cout << C (n, m) <<Endl; the } the return 0; the}
Toj 4111 combination number modulo violence decomposition