当前位置:网站首页>P5431 [template] multiplicative inverse 2
P5431 [template] multiplicative inverse 2
2022-06-11 07:09:00 【qq_ forty-six million five hundred and eighty thousand two hund】
P5431 【 Templates 】 Multiplicative inverse element 2
The question :

Solution:
This question is mainly about learning an idea .
s i = a 1 ∗ a 2 ∗ . . . ∗ a i 1 s i = 1 a 1 ∗ a 2 ∗ . . . ∗ a i 1 s i − 1 = 1 s i ∗ a i 1 a i = s i − 1 s i s_i=a_1*a_2*...*a_i \\ \frac{1}{s_i}=\frac{1}{a_1*a_2*...*a_i} \\ \frac{1}{s_{i-1}}=\frac{1}{s_i}*a_i \\ \frac{1}{a_i}=\frac{s_{i-1}}{s_i} si=a1∗a2∗...∗aisi1=a1∗a2∗...∗ai1si−11=si1∗aiai1=sisi−1
So for this question , Just find out s n − 1 s_n^{-1} sn−1, And then back out s i − 1 s_i^{-1} si−1, And the corresponding a i − 1 a_i^{-1} ai−1
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
char ch;
void read(int &x){
x=0;
for(ch=getchar();ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar())
x=(x<<3)+(x<<1)+(ch&15);
}
int n,p,k;
int a[5000005];
int ext_gcd(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1;y=0;
return a;
}
int d=ext_gcd(b,a%b,x,y);
int tem=y;
y=x-a/b*y;
x=tem;
return d;
}
int inv(int a,int b)
{
int x,y;
int d=ext_gcd(a,b,x,y);
x=(x%p+p)%p;
return x;
}
int Inv[5000005],b[5000005];
int main()
{
read(n);read(p);read(k);
int qaq=1;
for(int i=0;i<n;i++)
{
read(a[i]);
if(i==0)b[i]=a[i];
else b[i]=1ll*b[i-1]*a[i]%p;
}
qaq=inv(b[n-1],p);
for(int i=n-1;i>=0;i--)
{
if(i==0){
Inv[i]=qaq;break;}
Inv[i]=1ll*qaq*b[i-1]%p;
qaq=1ll*qaq*a[i]%p;
}
int res=0;
int q=1;
for(int i=0;i<n;i++)
{
q=1ll*q*k%p;
res=1ll*(res+1ll*Inv[i]*q%p)%p;
}
printf("%d",res);
return 0;
}
边栏推荐
- Saltstack deployment ZABBIX state file writing
- 微信小程序开发(原生和uniapp)DOM标签对比介绍
- Shell脚本之启动Nacos服务端
- 并发工具类
- 服务器调参实录
- 通过 Ingress 进行灰度发布
- 河南高考VS天津高考(2008年-2021年)
- Education expert wangzhongze shared his experience for many years: family education is not a vassal
- RGB-D Salient Object Detection withCross-Modality Modulation and Selection
- Start the Nacos server of shell script
猜你喜欢

Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)

Leetcode-141. Linked List Cycle

Starting from scratch (V) realize bullet positioning and animation

News web page display

About the designer of qtcreator the solution to the problem that qtdesigner can't pull and hold controls normally

Luogu p1091 chorus formation (longest ascending subsequence)

CMAP of Matplotlib

.NET C#基础(6):命名空间 - 有名字的作用域

saltstack部署lnmp

Latex various arrows / arrows with text labels / variable length arrows
随机推荐
农房一体脚本的心得记录
Leetcode-647. Palindromic Substrings
Bat (batch processing) processing special symbols (exclamation point, percent sign), etc
Difference between byte and bit
LEARNING TARGET-ORIENTED DUAL ATTENTION FOR ROBUST RGB-T TRACKING
Whether the ZABBIX monitoring host is online
Notes on learning Es5 and ES6
Promises/a+ standard Chinese Translation
The meaning and research significance of mathematical methodology
. Net C Foundation (6): namespace - scope with name
Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
Object. Specific implementation and difference between create() and new
Shutter restraint container assembly
Leetcode-141. Linked List Cycle
1300. the array closest to the target value after transforming the array and
Array information management system reconfiguration preheating (1) how to write basic logic using linear continuous structure?
The difference between TCP and UDP
Summary of string processing skills III
[Xunwei dry goods] opencv test of Godson 2k1000 development board