#include<cstdio>
typedef long long ll;
int n, p;
ll q_pow(ll a, int k, int p) {
ll ret = 1;
for(; k; k >>= 1) {
if(k&1) ret = ret*a%p;
a = a*a%p;
}
return ret;
}
ll inv(intx) {
return q_pow(x, p-2, p);
}
int main() {
scanf("%d%d", &n, &p);
for(int i = 1; i <= n; ++i) printf("%lld\n", inv(i));
return0;
}
乘法逆元(递推)
#include<cstdio>
const int N = 3e6 + 5;
int n, p, inv[N];
int main() {
scanf("%d%d", &n, &p);
printf("1\n");
inv[1] = 1;
for(int i = 2; i <= n; ++i) {
inv[i] = (long long)(p-p/i) * inv[p%i]%p;
printf("%d\n", inv[i]);
}
return0;
}
乘法逆元(拓展欧几里得)
//ax=1(mod b) -> ax+by=1#include<cstdio>int exgcd(int a, int b, int &x, int &y) {
if(!b) {
x = 1; y = 0;
return a;
}
int Gcd = exgcd(b, a%b, x, y);
int tmp = x;
x = y;
y = tmp - a/b*y;
return Gcd;
}
int main() {
intx, y, a, b;
scanf("%d%d", &a, &b);
exgcd(a, b, x, y);
printf("%d\n", (x+b)%b);
return0;
}
组合数取模(卢卡斯定理)
#include<cstdio>typedeflonglong ll;
constint N = 1e5 + 10;
ll a[N], n, m, p, kase;
ll q_pow(ll a, int b, int p) {
ll ret = 1; a%=p;
for(; b; b>>=1) {
if(b&1) ret = ret*a%p;
a = a*a%p;
}
return ret;
}
ll inv(ll x, int p) {
return q_pow(x, p-2, p);
}
ll C(ll n, ll m) {
if(m > n) return0;
return ((a[n]*inv(a[m], p)%p * inv(a[n-m], p))%p);
}
ll Lucas(ll n, ll m) {
if(!m) return1;
return C(n%p, m%p) * Lucas(n/p, m/p)%p;
}
int main() {
scanf("%d", &kase);
while(kase--) {
scanf("%d%d%d", &n, &m, &p);
a[0] = 1;
for(int i = 1; i <= p; ++i) a[i] = (a[i-1]*i) % p;
printf("%lld\n", Lucas(n+m, n));
}
}