当前位置:网站首页>[luogu_p5431] [template] multiplicative inverse 2 [number theory]
[luogu_p5431] [template] multiplicative inverse 2 [number theory]
2022-07-27 13:58:00 【VL——MOESR】

Ideas :
Obviously, let's ask ai Inverse element
Then just push :
We set up si by ai Prefix product of
So easy to get :
1 s i − 1 = 1 s i × a i \frac{1}{s_{i-1}}=\frac{1}{s_i}×a_i si−11=si1×ai
namely
1 a i = s i − 1 s i \frac{1}{a_i}=\frac{s_{i-1}}{s_i} ai1=sisi−1
First of all sn The inverse of is solved by Fermat's theorem , And then linearly recurs back s Array , namely :
1 s i − 1 = 1 s i × a i \frac{1}{s_{i-1}}=\frac{1}{s_i}×a_i si−11=si1×ai
hold s After finding the inverse of , You can recurse linearly a The inverse of
c o d e code code
#include<iostream>
#include<cstdio>
#define ll long long
#define re register
using namespace std;
const ll MAXN = 5e6 + 10;
ll n, p, k;
ll s[MAXN], b[MAXN], a[MAXN];
inline int read() {
ll s=0, w=1;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-') w = -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
inline ll qpow(ll x, ll k) {
re ll ans = 1;
while(k) {
if(k & 1) ans = ans * x % p;
x = x * x % p;
k >>= 1;
}
return ans;
}
int main() {
n = read(), p = read(), k = read();
s[0] = 1;
for(re ll i = 1; i <= n; ++ i) {
a[i] = read();
s[i] = s[i - 1] * a[i] % p;
}
b[n + 1] = qpow(s[n], p - 2);
for(re ll i = n; i >= 1; -- i)
b[i] = b[i + 1] * a[i] % p;
re ll q = 1, ans = 0;
for(re ll i = 1; i <= n; ++ i)
q = q * k % p, ans = (ans + q * s[i - 1] % p * b[i + 1]) % p;
printf("%lld", ans);
return 0;
}
边栏推荐
- VSCode -- 创建模板文件
- new的多种使用方法
- Design of network abnormal traffic analysis system
- Experience sharing of system architecture designers preparing for the exam: a tough battle for nearly three months
- egg-swagger-doc 图形验证码解决方案
- List map basic notes
- The salary level of programmers in various countries is a little miserable
- 看看有没有你,各赛区入围名单
- WPF visifire.charts4.6.1 tutorial with source code
- Gray histogram
猜你喜欢

JS 模块、闭包应用

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

SQL教程之 SQL 聚合函数入门教程

Dat.gui control custom placement and dragging

Product manager experience 100 (XI) - Strategic Product Manager: model and methodology

Wechat campus laundry applet graduation design finished product (4) opening report

Thinkphp+ pagoda operation environment realizes scheduled tasks

Design of LR1 compiler based on C language

【实习经验】Date工具类中添加自己实现的方法

我们要学会查看技术细节点的文档化说明
随机推荐
Data enhancement in image processing
【每日一题】1206. 设计跳表
我们要学会查看技术细节点的文档化说明
[training day3] section [greed] [two points]
There is no need for semantic segmentation of annotation data! Eth & Leuven University proposed maskdistill, using transformer for unsupervised semantic segmentation, SOTA
A Keypoint-based Global Association Network for Lane Detection
软考 系统架构设计师 简明教程 | 软件测试
Jianzhi offer 07 rebuild binary tree -- construct binary tree from middle order and post order traversal sequence
Charles tutorial
RSS tutorial: aggregate your own information collection channels, rshub, freshrss, NetNewsWire
2022acm summer training weekly report (IV)
[daily question] 1206. Design jump table
Leetcode Tencent selected exercises 50 questions -054. spiral matrix
JWT login expiration - automatic refresh token scheme introduction
Redis implements the browsing history module
小程序毕设作品之微信校园洗衣小程序毕业设计成品(8)毕业设计论文模板
期货公司开户后续会有哪些服务?
See if you are on the shortlist of each division
In the "meta cosmic space", utonmos will open the digital world of the combination of virtual and real
Download address of each version of libtorch