This question is a solution to a problem from the purple book, here to write about the deepening impact
Gives the N, M satisfying M <= N is obtained 2-N! Present in all x such that x number of prime factors greater than M, then all prime factors greater than M and M is equivalent to! Relatively prime, Euclidean algorithm,
We x and M! Relatively prime converted to x% M! M and! Relatively prime, so we will only need to enumerate smaller than M! Number, then the number obtained * N! / M! (Mean increase in number a M! still satisfy the condition), to obtain less than M! M and the number! relatively prime, we use is the Euler function , and that we can find phifac (n with phifac (n) = phi (n !)) relationship phifac (n-1) with their prime factors tend to be the same, if n is not a prime number, phifac (n) = phifac ( n-1) * n otherwise phifac (n) = phifac (n -1) * ( n-1)
This resulted in the size phifac (n) of the note overflow, I am not particularly clear some of the details, here's the code:
// UVa 11440 #include <cstdio> #include <cstring> #include <cmath> using namespace std; const int maxn = 10000000 + 5; const int MOD = 100000007; int vis[maxn], phifac[maxn]; void Eratosthenes(int n) { memset(vis, 0, sizeof(vis)); int m = sqrt(n + 0.5); for (int i = 2; i <= m; ++i) for (int j = i*i; j <= n; j += i) vis[j] = 1; } int main() { int n, m; Eratosthenes(10000001); phifac[1] = phifac[2] = 1; for (int i = 3; i <= 10000001; ++i) phifac[i] = (long long)phifac[i-1] * (vis[i] ? i : i-1) % MOD; while (scanf("%d%d", &n, &m) == 2 && n) { int ans = phifac[m]; for (int i = m+1; i <= n; ++i) ans = (long long)ans * i % MOD; printf("%d\n", (ans-1+MOD)%MOD); } return 0; }