全WA求调
查看原帖
全WA求调
877829
Anythingispossible楼主2024/8/13 17:24
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=5e6+10;
ll n,k,a[N],p,t=1,ans,suf[N],inv=1,sum[N];


inline ll read(){
	ll x=0,f=1;
	char ch;
	ch=getchar();
	while(ch<'0'&&ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}//快读 

inline void write(ll x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x>9) write(x/10);
	putchar(x%10+'0');
	return ;
}//快写 

inline ll qpow(ll x){
	ll ypy=p-2,res=1;
	while(ypy){
		if(ypy&1) res=(res*x)%p;
		x=(x*x)%p;
		ypy=ypy>>1;
	}
	return res%p;
}//费马小定理求逆元 

int main(){
	n=read(),p=read(),k=read();
	for(int i=1;i<=n;++i){
		a[i]=read();
		inv=(inv*a[i])%p;
	}
	
	inv=qpow(inv)%p;//分母求逆元 
	
	sum[1]=1;
	for(int i=2;i<=n;++i){
		sum[i]=(sum[i-1]*a[i-1])%p;
	}//前缀积
	 
	suf[n]=1;
	for(int i=n-1;i>=1;--i){
		suf[i]=(a[i+1]*suf[i+1])%p;
	}//后缀积 
	
	for(int i=1;i<=n;++i){
		t=(t*k)%p;//处理k^i 
		ans=(ans%p+sum[i]%p*suf[i]%p*t%p)%p;
	} 
	ans=ans*inv%p;
	write(ans%p);
	return 0;
}
2024/8/13 17:24
加载中...