1951: [sdoi2010] Ancient pig
Link: Click Here ~
Question:
A good combination of mathematical questions !!!! The question is very long, but it is useful in the following paragraphs.
Ipig thinks that as long as it complies with the document, it is possible for each K to divide n. He intends to consider all possible K. Obviously, when K is equal to a specific value, the number of pig texts in this direction is N/K. However, N/k characters are left in N texts. Ipig predicts that if the total number of all possible K cases is P, then the price of his study of ancient words will be the power of G's p. Now he wants to know the price of studying ancient Chinese characters in the pig kingdom. Because ipig thinks this number may be an astronomical number, you only need to tell him the answer divided by the remainder of 999911659.
That is, P = C (n, I | N) is required, and then G ^ P % 999911659 is the final answer.
We can easily see that the problem is composed of two parts, solving G ^ P and P.
We can easily conclude that G ^ P can be easily solved by using a fast power. Now, how can we quickly find P? This is a problem! We continue to break down and find that the composition of P is absent. Yes! Is the combination formula C (n, I | N )! We can also know that the combination formula can be obtained using the brute force formula of the Yang Hui triangle if the data is small. However, if it is a big union, we need to use the Lucas theorem. (The aekdycoin space provides a detailed description in this regard) and the number of combinations of this question is obviously a large number of groups. And directly seeking for benefits even long !! What should I do? We can think of the common processing method of a large number of groups, that is, modulo.
Module? Where is the model? At this time, we have the Fermat theorem that we can know when P is a prime number:
G ^ P % mod = G ^ (P % (mod-1) % mod
Therefore, mod-1 = 999911658 is an even number, and it does not conform to the Lucas theorem. Therefore, we need to perform a prime number decomposition for this even number. 999911658 = 2*3*4679*35617 but now the problem is coming. After you break down the modulo, how can we find the result?
Let's take a good look. What have you found? Yes! There is an equality relationship as follows:
X = a1 % M1
X = a2 % m2
X = A3 % m3
.
.
.
.
Yes! It is the China residue theorem!
Now, after a step-by-step decomposition, the problem is finally solved. You should understand it! A question !!
/* Algorithm: number of combinations of Modulo non-prime numbers. Question: P = C (n, I | N) is required ), then, G ^ P % 999911659 1 is obtained, the modulo factor is decomposed 2, and the Chinese Remainder Theorem 1 <= G <= 10 ^ 9,1 <= n <= 10 ^ 9 */# include <iostream> # include <algorithm> # include <cstdio> # include <cstring> # include <cmath> using namespace STD; typedef long ll; const int Phi = 999911658; const int mod = 999911659; const int maxn = 200000; struct prim {int CNT, Prim [maxn], num [maxn];} PZ, NZ; int N, G; ll a [maxn]; ll fact [5] [37777]; // Factorization prime factor void Div (prim & P, int X) {P. CNT = 0; // initialize int K = SQRT (x + 0.5); For (INT I = 2; I <= K; ++ I) {If (0 = x % I) {P. num [++ p. CNT] = 0; p. prim [p. CNT] = I; while (0 = x % I) {++ p. num [p. CNT]; X/= I ;}}if (x> 1) {P. num [++ p. CNT] = 1; p. prim [p. CNT] = x ;}// preprocessing factorial void Init () {int I, j; for (I = 1; I <= PZ. CNT; ++ I) {fact [I] [0] = 1; for (j = 1; j <= PZ. prim [I]; ++ J) fact [I] [J] = fact [I] [J-1] * j % PZ. prim [I];} // Fast power modulo ll powmod (ll a, LL B, const Int & mod) {ll res = 1; A % = MOD; while (B> 0) {If (B & 1) RES = res * A % MOD; A = A * A % MOD; B >>= 1;} return res;} ll norma_c (INT N, int M, int I) {int P = PZ. prim [I]; return fact [I] [N] * powmod (fact [I] [m] * fact [I] [n-M] % P, P-2, P) % P;} // lucasll Lucas (int n, int M, int I) {If (! M) return 1; int P = PZ. prim [I]; If (N % P <m % P) return 0; ll TMP = norma_c (N % P, M % P, I ); return TMP * Lucas (N/P, M/P, I) % P;} // calculate C (n, I) void deal (INT sum) {for (INT I = 1; I <= PZ. CNT; ++ I) {A [I] = (a [I] + Lucas (n, sum, I) % PZ. prim [I] ;}}// find I | nvoid DFS (INT DEP, int sum) {If (DEP> NZ. CNT) {deal (SUM); return;} DFS (DEP + 1, sum); // do not use this number for (INT I = 1; I <= NZ. num [Dep]; ++ I) {// sum * = NZ. prim [Dep]; DFS (DEP + 1, sum) ;}// Extended Euclidean void extgcd (ll a, LL B, ll & D, ll & X, ll & Y) {If (! B) {d = A; X = 1; y = 0;} else {extgcd (B, A % B, D, Y, X ); y-= x * (a/B) ;}// China Remainder Theorem ll China () {ll M = Phi, D, Y, x = 0; for (INT I = 1; I <= PZ. CNT; ++ I) {ll W = m/PZ. prim [I]; extgcd (Pz. prim [I], W, D, D, Y); X = (x + y * w * A [I]) % m;} X = (x + M) % m; return X;} int main () {While (~ Scanf ("% d", & N, & G) {memset (A, 0, sizeof (a); G % = MOD; If (! G) {puts ("0"); continue ;} /// // Div (PZ, Phi ); // perform qualitative factor decomposition Div (NZ, n) on the modulo; // perform qualitative factor decomposition Init () on the logarithm; // pre-process factorial DFS ); ll ans = China (); // China residual theorem // printf ("% LLD \ n ", powmod (LL) g, ANS, MoD); // G ^ p} return 0 ;}
1951: [sdoi2010] Ancient pig