比赛 NOIP模拟赛by mzx Day2 评测结果 AAAATTTTEE
题目名称 学姐的巧克力盒 最终得分 40
用户昵称 再见 运行时间 8.755 s
代码语言 C++ 内存使用 61.33 MiB
提交时间 2016-10-20 21:33:49
显示代码纯文本
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#define lc(o) o<<1
#define rc(o) o<<1|1

typedef long long int LL;

inline int read(){
	int _x=0; char ch; bool flag=false;
	while(ch=getchar(),!isdigit(ch)) ch=='-'?flag=true:false;
	while(_x=_x*10+ch-'0',ch=getchar(),isdigit(ch));
	return flag?-_x:_x;
}

char out[100];int len;
inline void print(LL x){
	if(x==0) putchar('0');
	len=0;
	while(x!=0){
		out[len++]=x%10+'0';
		x/=10;
	}
	while(len){
		len--;
		putchar(out[len]);
	}
	putchar('\n');
}

int n,m;
LL phi,k,p1,p2,mul[4000010][2];

void build(int o,int l,int r){
	if(l==r){
		mul[o][1]=mul[o][0]=read();
		mul[o][0]%=p1;
		mul[o][1]%=phi;
		return;
	}
	int mid=(l+r)>>1;
	build(lc(o),l,mid);
	build(rc(o),mid+1,r);
	mul[o][0]=(mul[lc(o)][0]*mul[rc(o)][0])%p1;
	mul[o][1]=(mul[lc(o)][1]*mul[rc(o)][1])%phi;
}

LL query(int o,int l,int r,int ql,int qr){
	if(ql<=l&&r<=qr) return mul[o][0];
	else{
		int mid=(l+r)>>1; LL re=1;
		if(ql<=mid) re=(re*query(lc(o),l,mid,ql,qr))%p1;
		if(mid<qr) re=(re*query(rc(o),mid+1,r,ql,qr))%p1;
		return re;
	}
}

LL query2(int o,int l,int r,int ql,int qr){
	if(ql<=l&&r<=qr) return mul[o][1]%phi;
	else{
		int mid=(l+r)>>1; LL re=1;
		if(ql<=mid) re=(re*query2(lc(o),l,mid,ql,qr))%phi;
		if(mid<qr) re=(re*query2(rc(o),mid+1,r,ql,qr))%phi;
		return re;
	}
}

LL pow2(LL a,LL b){
	if(b==0) return 1;
	LL re=pow2(a,b/2);
	re=(re*re)%p2;
	if(b&1) re=(re*a)%p2;
	return re;
}

inline void getphi(){
	int x=p2,y=sqrt(x);phi=1;
	for (int i=2;i<=y;i++)
		if(x%i==0){
			int p=i-1; x/=i;
			while(x%i==0) x/=i,p*=i;
			phi*=p;
		}
	if(x!=1) phi=phi*(x-1);
}

int main()
{
	//freopen("a.txt","r",stdin);
	freopen("chocolatebox.in","r",stdin);
	freopen("chocolatebox.out","w",stdout);
	n=read(); m=read(); scanf("%lld%lld%lld",&k,&p1,&p2);
	getphi();build(1,1,n);
	while(m--){
		int p=read(),l=read(),r=read();
		if(p&1){
			LL ans=query(1,1,n,l,r);
			print(ans);
		}
		else{
			LL ans=query2(1,1,n,l,r);
			LL ansout=pow2(k,ans);
			print(ansout);
		}
	}
	return 0;
}