• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jacklee404
Never Stop!
博客园    首页    新随笔    联系   管理    订阅  订阅
P5431 模意义下的乘法逆元

P5431 模意义下的乘法逆元

思路

求前缀积,然后求第\(n\) 个数的逆元

\(sv[i - 1] = sv[i] * a[i]\)最后在根据 \(inv[i] = prefix[i - 1] * sv[i]\) 即可。

代码

#include <bits/stdc++.h>

using i64 = long long;

const int N = 5e6 + 10;

int n, p, k, arr[N], prefix[N], sv[N], K[N];

int fast_pow(int a, int b, int mod) {
	int res = 1;
	while (b) {
		if (b & 1) res = (i64) res * a % mod;
		a = (i64) a * a % mod;
		b >>= 1;
	}
	return res;
}

template<class T>
T read(T &x){
    x=0;char ch=0;
    while(ch < '0' || ch > '9')ch=getchar();
    while(ch >= '0' && ch <= '9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}

int main() {
	read(n), read(p), read(k);

	prefix[0] = K[0] = 1;

	for (int i = 1; i <= n; i ++) {
		read(arr[i]);
		prefix[i] = (i64) arr[i] * prefix[i - 1] % p;
		K[i] = (i64) K[i - 1] * k % p;	
	} 

	sv[n] = fast_pow(prefix[n], p - 2, p);

	i64 res = 0;


	for (int i = n; i >= 1; i --) {
		sv[i - 1] = (i64) sv[i] * arr[i] % p;
		res += (((i64) sv[i] * prefix[i - 1] % p) * K[i])% p;
		res %= p;
	}

	printf("%lld", res);
}
posted on 2023-10-29 12:05  Jack404  阅读(18)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3