Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = contents by --- cxlove
Question: Use all numbers not greater than N to form a full number as large as possible.
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4196
The number of complete distinct values is obviously the number of all prime factors is an even number, and N is taken! To calculate the number of all prime factors! If the number of prime factors is exceeded for an odd number, all the even numbers are required, and then all are multiplied. However, because of the large scale, even the rapid power multiplication will time out.
So consider n! To remove the product of the odd number of factors is to obtain a = C/B. Because it is a modulo, it is inevitable that the Division will not work.
Consider C % mod = (a % mod) * (B % mod); Make A = A % MOD; B = B % MOD; C = C % MOD;
Then a = A % mod = (A * 1) % mod = A % mod * 1% mod = (a % mod) * (B ^ (mod-1 )) % mod -------- because (B ^ (mod-1) % mod = 1, ferma's theorem.
= (A * B) % mod * (B ^ (mod-2) % mod = (C % mod) * (B % mod) ^ (mod-2) % mod = C * B ^ (mod-2)
With this, we can find the result of C/B.
The steps for solving the problem are:
1. base number table
2. calculate n! Contains the number of prime factors. If it is an odd number, the product of this prime factor is retained for the final division.
3. Obtain the division result.
# Include <iostream> # include <cstdio> # include <cmath> # define n 10000000 # define mod 1000000007 # define ll long longusing namespace STD; bool flag [n + 5] = {0}; int prime [1000000], CNT = 0; void prime () {for (INT I = 2; I <SQRT (n + 1.0); I ++) {If (flag [I]) continue; For (Int J = 2; j * I <= N; j ++) Flag [I * j] = true ;}for (INT I = 2; I <= N; I ++) if (! Flag [I]) prime [CNT ++] = I;} int getsum (INT N, int p) {int sum = 0; while (N) {sum + = N/P; N/= P;} return sum;} ll powmod (ll a, LL B) {ll ans = 1; while (B) {If (B & 1) ans = (ANS * A) % MOD; A = (A * A) % MOD; B >>= 1;} return ans ;} ll FAC [n + 5]; int A; int main () {int N; FAC [1] = 1; for (INT I = 2; I <= N; I ++) FAC [I] = (FAC [I-1] * I) % MOD; prime (); While (scanf ("% d", & N )! = EOF & N) {ll ret = 1; for (INT I = 0; I <CNT & prime [I] <= N; I ++) {A = getsum (n, prime [I]); If (A & 1) ret = (Ret * prime [I]) % MOD ;} printf ("% i64d \ n", (FAC [N] * powmod (Ret, mod-2) % mod);} return 0 ;}