第一种 利用前缀求n个数的逆元
#include<bits/stdc++.h>
#include<unordered_map>
#include<math.h>
#include<forward_list>
#include<unordered_set>
using namespace std;
#pragma warning(disable:4996)
const double pi = acos(-1);
#define pdd pair<double,double>
#define re register
#define ll long long
#define endl '\n'
#define pii pair<ll,ll>
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define se second
#define ull unsigned ll
#define eb emplace_back
#define pb push_back
#define rad read
#define mp make_pair
#define ls (o<<1)
#define rs (o<<1|1)
#define mid (l+r>>1)
#define fpn(x) fp(x),putchar('\n');
ll mod = 80112002;
inline ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
inline ll exgcd(ll a, ll b, ll& x, ll& y) {
//ax+by=1; 返回值为a,b最大公约数
if (!b) {
x = 1, y = 0;
return a;
}
ll g= exgcd(b, a % b, y, x);
y -= (a / b) * x;
return g;
}
inline ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
while (b) {
if (b & 1) ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans;
}
ll qpow(ll a, ll b, ll mod) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ksc(ans, a, mod);
a = ksc(a, a, mod);
b >>= 1;
}
return ans;
}
inline ll read() {
ll s = 0; bool w = true;
char c = getchar();
for (; c < '0' || c>'9'; c = getchar())if (c == '-')w = false;
for (; c >= '0' && c <= '9'; c = getchar())s = (s << 1) + (s << 3) + (c ^ 48);
return w ? s : -s;
}
inline void fp(ll n) {
if (n < 0) putchar('-'), n = -n;
if (n > 9) fp(n / 10);
putchar(n % 10 + 48);
}
const int N = 3e6 + 10;
ll inv[N], s[N],a[N];
void solve() {
ll n = read();
for (int i = 1; i <= n; ++i)a[i] = read();//求a1-an的逆元
ll p = read();
inv[1] = s[1] = 1;
for (int i = 2; i <= n; ++i)
s[i] = (s[i - 1] % p * a[i] % p) % p;//改为 s[i - 1] * i 的话,下面也要改
inv[n] = qpow(s[n], p - 2, p);
for (int i = n - 1; i >= 1; --i)
inv[i] = (inv[i + 1] % p * a[i+1] % p) % p;// inv[i + 1] * (i + 1)
for (int i = 2; i <= n; ++i)
inv[i] = (inv[i] % p * s[i - 1] % p) % p;
for (int i = 1; i <= n; ++i)fpn(inv[i]);
}
signed main()
{
ll T = 1;
// T = read();
while (T--) {
solve();
}
return 0;
}
/**
* ┏┓ WQJ ┏┓+ +
* ┏┛┻━━━┛┻┓ +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓>>>
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
第二种 线性求逆元(1-n)洛谷模板题
#include<bits/stdc++.h>
#include<unordered_map>
#include<math.h>
#include<forward_list>
#include<unordered_set>
using namespace std;
#pragma warning(disable:4996)
const double pi = acos(-1);
#define pdd pair<double,double>
#define re register
#define ll long long
#define endl '\n'
#define pii pair<ll,ll>
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define se second
#define ull unsigned ll
#define eb emplace_back
#define pb push_back
#define rad read
#define mp make_pair
#define ls (o<<1)
#define rs (o<<1|1)
#define mid (l+r>>1)
#define fpn(x) fp(x),putchar('\n');
ll mod = 80112002;
inline ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
inline ll exgcd(ll a, ll b, ll& x, ll& y) {
//ax+by=1; 返回值为a,b最大公约数
if (!b) {
x = 1, y = 0;
return a;
}
ll g= exgcd(b, a % b, y, x);
y -= (a / b) * x;
return g;
}
inline ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
while (b) {
if (b & 1) ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans;
}
ll qpow(ll a, ll b, ll mod) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ksc(ans, a, mod);
a = ksc(a, a, mod);
b >>= 1;
}
return ans;
}
inline ll read() {
ll s = 0; bool w = true;
char c = getchar();
for (; c < '0' || c>'9'; c = getchar())if (c == '-')w = false;
for (; c >= '0' && c <= '9'; c = getchar())s = (s << 1) + (s << 3) + (c ^ 48);
return w ? s : -s;
}
inline void fp(ll n) {
if (n < 0) putchar('-'), n = -n;
if (n > 9) fp(n / 10);
putchar(n % 10 + 48);
}
const int N = 3e6 + 10;
ll inv[N], s[N],a[N];
void solve() {
ll n = read();
ll p = read();
inv[1] = 1;
for (int i = 2; i <= n; ++i)inv[i] = ((p - p / i) % p * inv[p % i] % p) % p;//不能调用快速乘,会超时
for (int i = 1; i <= n; ++i)fpn(inv[i]);
}
signed main()
{
ll T = 1;
// T = read();
while (T--) {
solve();
}
return 0;
}
/**
* ┏┓ WQJ ┏┓+ +
* ┏┛┻━━━┛┻┓ +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓>>>
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
第三种,利用exgcd(二元函数的解求逆元),gcd(a,p)=1,p不为素数也可
#include<bits/stdc++.h>
#include<unordered_map>
#include<math.h>
#include<forward_list>
#include<unordered_set>
using namespace std;
#pragma warning(disable:4996)
const double pi = acos(-1);
#define pdd pair<double,double>
#define re register
#define ll long long
#define endl '\n'
#define pii pair<ll,ll>
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define se second
#define ull unsigned ll
#define eb emplace_back
#define pb push_back
#define rad read
#define mp make_pair
#define ls (o<<1)
#define rs (o<<1|1)
#define mid (l+r>>1)
#define fpn(x) fp(x),putchar('\n');
ll mod = 80112002;
inline ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
inline ll exgcd(ll a, ll b, ll& x, ll& y) {
//ax+by=1; 返回值为a,b最大公约数
if (!b) {
x = 1, y = 0;
return a;
}
ll g= exgcd(b, a % b, y, x);
y -= (a / b) * x;
return g;
}
inline ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
while (b) {
if (b & 1) ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans;
}
ll qpow(ll a, ll b, ll mod) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ksc(ans, a, mod);
a = ksc(a, a, mod);
b >>= 1;
}
return ans;
}
inline ll read() {
ll s = 0; bool w = true;
char c = getchar();
for (; c < '0' || c>'9'; c = getchar())if (c == '-')w = false;
for (; c >= '0' && c <= '9'; c = getchar())s = (s << 1) + (s << 3) + (c ^ 48);
return w ? s : -s;
}
inline void fp(ll n) {
if (n < 0) putchar('-'), n = -n;
if (n > 9) fp(n / 10);
putchar(n % 10 + 48);
}
const int N = 3e6 + 10;
ll inv[N], s[N],a[N];
void solve() {
//求1-n的逆元
ll n = read();
ll p = read();
inv[1] = 1;
for (int i = 2; i <= n; ++i) {
ll x = 0, y = 0;
exgcd(i, p, x, y);//ix+py=gcd(i,p)(%p) 此处x=i的-1次方(即i的逆元)
inv[i] = (x+p)%p; //确保它为整数
}
for (int i = 1; i <= n; ++i)fpn(inv[i]);
}
signed main()
{
ll T = 1;
// T = read();
while (T--) {
solve();
}
return 0;
}
/**
* ┏┓ WQJ ┏┓+ +
* ┏┛┻━━━┛┻┓ +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓>>>
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/