Bzoj mathematical Number Theory
Question: Find 1 ~ N! Medium and M! Ensure n> M.
I hate mathematics most...
Idea: n> M is ensured, so n! It must be m! . If an X is found to make gcd (x, M !) = 1, then gcd (x + M !, M !) = 1 must be true, gcd (x + K * m !, M !) = 1 (k> = 1) is also valid. The number of X is PHI (M !), Then the total number is PHI (M !) * N! /M !. Then various formulas can be used to solve this problem through linear screening. (I am too weak to understand...
Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 10000100using namespace std;int T,P,n,m,primes;long long prime[500100],ans[MAX],_link[MAX],rev[MAX];bool notp[MAX];void Pretreatment(){for(int i = 2; i < MAX; ++i) {if(!notp[i])prime[++primes] = i;for(int j = 1;j <= primes && prime[j] * i < MAX; ++j) {notp[prime[j] * i] = true;if(i % prime[j] == 0)break;}}_link[0] = 1;for(int i = 1; i < MAX; ++i)_link[i] = _link[i - 1] * i % P;rev[1] = 1;for(int i = 2; i < MAX && i < P; ++i)rev[i] = (P - P / i) * rev[P % i] % P;ans[1] = 1;for(int i = 2; i < MAX; ++i) {if(!notp[i])ans[i] = ans[i - 1] * (i - 1) % P * rev[i % P] % P;elseans[i] = ans[i - 1];}}int main(){cin >> T >> P;Pretreatment();for(int i = 1; i <= T; ++i) {scanf("%d%d",&n,&m);printf("%d\n",_link[n] * ans[m] % P);}return 0;}
Bzoj 2186 sdoi 2008 confused number theory of Princess salad