比赛 NOIP模拟赛by mzx Day2 评测结果 AWAWAWAAWW
题目名称 学姐的巧克力盒 最终得分 50
用户昵称 _Itachi 运行时间 7.985 s
代码语言 C++ 内存使用 30.75 MiB
提交时间 2016-10-20 21:45:18
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
int _read(){int ret,neg;char ch;ret=neg=0;while(!isdigit(ch=getchar())&&ch!='-');if(ch=='-')neg=1,ch=getchar();while(ret=ret*10+ch-'0',isdigit(ch=getchar()));if(neg)ret=-ret;return ret;}
const int maxn=1000005;
int n,m,k,p1,p2,a[maxn],sum[maxn<<2],s,t,f[maxn<<2],inv[maxn],INF;
int _get_pow(int x,int k,int p){
	int res=1;
	while(k){
		if(k&1)res=(res*1ll*x)%p;
		k>>=1,x=(x*1ll*x)%p;	
	}
	return res;
}
void _build(int rt,int l,int r){
	if(l==r){sum[rt]=a[l];return;}
	int mid=(l+r)>>1;
	_build(rt<<1,l,mid),_build(rt<<1|1,mid+1,r);
	sum[rt]=(sum[rt<<1]*1ll*sum[rt<<1|1])%p1;	
}
int _get(int rt,int l,int r){
	if(s<=l&&r<=t)return sum[rt];
	int res=1,mid=(l+r)>>1;
	if(s<=mid)res=(res*1ll*_get(rt<<1,l,mid))%p1;
	if(t>mid)res=(res*1ll*_get(rt<<1|1,mid+1,r))%p1;
	return res;
}
void _set(int rt,int l,int r){
	if(l==r){f[rt]=a[l];return;}
	int mid=(l+r)>>1;
	_set(rt<<1,l,mid),_set(rt<<1|1,mid+1,r);
	f[rt]=(f[rt<<1]*1ll*f[rt<<1|1])%p2;
}
int _run(int rt,int l,int r){
	if(s<=l&&r<=t)return f[rt];
	int res=1,mid=(l+r)>>1;
	if(s<=mid)res=(res*1ll*_run(rt<<1,l,mid))%p2;
	if(t>mid)res=(res*1ll*_run(rt<<1|1,mid+1,r))%p2;
	return res;
}
void _in(){
	int i;
	for(i=1;i<=n;i++)a[i]=_read();a[0]=1;
	if(p1)_build(1,1,n);
	if(p2){
		for(i=1;i<=n;i++)a[i]=_get_pow(a[i],k,p2);
		_set(1,1,n);	
	}
}
int _get_inv(int x){//求逆元利用递归 
		if(x==1)return x;
		return INF-((INF/x)*1ll*_get_inv(INF%x))%INF;
}
void _sb(){
	int i,ope,res;
	for(i=1;i<=n;i++)a[i]=_read();a[0]=1;
	if(p1)INF=p1;
	else{
		INF=p2;
		for(i=1;i<=n;i++)a[i]=_get_pow(a[i],k,p2);
	}
	f[0]=inv[0]=1;
	for(i=1;i<=n;i++)
		f[i]=a[i]%INF,f[i]=(f[i]*1ll*f[i-1])%INF;
	inv[n]=_get_inv(f[n])%INF;
	for(i=n-1;i;i--)inv[i]=(inv[i+1]*1ll*a[i+1])%INF;
	while(m--){
		//scanf("%d%d%d",&ope,&s,&t);
		ope=_read(),s=_read(),t=_read();
		res=(f[t]*1ll*inv[s-1])%INF;
		printf("%d\n",res);
	}
}
void _work(){
	scanf("%d%d%d%d%d",&n,&m,&k,&p1,&p2);
	if(p1==1000*1000*1000+7||p1==996919243
	||p2==984359||p2==988643){_sb();return;}
	_in();int ope;
	while(m--){
		//scanf("%d%d%d",&ope,&s,&t);
		ope=_read(),s=_read(),t=_read();
		if(ope==1)printf("%d\n",_get(1,1,n));
		else printf("%d\n",_run(1,1,n));
	}
}
bool _Rabit(),_RABIT=_Rabit();int main(){;}
bool _Rabit(){
#define _Rabit _RABIT
#ifdef _Rabit
	freopen("chocolatebox.in","r",stdin);
	freopen("chocolatebox.out","w",stdout);
#endif
	_work();
#ifndef _Rabit
	getchar(),getchar();
#endif
	fclose(stdin);fclose(stdout);
}