题目链接:https://www.luogu.org/problemnew/show/P3811
题目大意:给定
n
,
p
n,p
n,p 求
1
−
n
1-n
1−n 中所有整数在模p意义下的乘法逆元。
个人感想
输出用cout居然TLE
逆元递推inv[i] = (p - p / i)*inv[p%i] % p
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll a, b, m, x, y, n, p;
ll inv[3000000 + 100];
ll exgcd(ll a, ll b, ll &x, ll &y){
if (b == 0) { x = 1, y = 0; return a; }
ll d = exgcd(b, a%b, x, y);
ll z = x; x = y; y = z - y * (a / b);
return d;
}
int main()
{
scanf("%lld%lld",&n,&p);
inv[1] = 1;
cout << 1 << endl;
for (ll i = 2; i <= n; i++){
inv[i] = (p - p / i)*inv[p%i] % p;
printf("%lld\n",inv[i]);
//cout << inv[i] << endl;
}
return 0;
}