1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<cctype> #define int long long using namespace std; inline int read(){ int w=0,x=0;char c=getchar(); while(!isdigit(c))w|=c=='-',c=getchar(); while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar(); return w?-x:x; } namespace star { const int maxn=5e5+10,maxp=2e7+10; int mod,n,m,phi[maxp],tot,prime[maxp/10],cnt,a[maxn]; inline int lowbit(int x){return x&(-x);} inline void update(int x,int y){ for(;x<=n;x+=lowbit(x))a[x]+=y; } inline int query(int x){ int ans=0; for(;x;x-=lowbit(x))ans+=a[x]; return ans; } bool mark[maxp]; inline void pre(int n){ phi[1]=1; for(int i=2;i<=n;i++){ if(!mark[i])prime[++cnt]=i,phi[i]=i-1; for(int j=1;j<=cnt and i*prime[j]<=n;j++){ mark[i*prime[j]]=1; if(!(i%prime[j])){ phi[i*prime[j]]=phi[i]*prime[j]; break; } phi[i*prime[j]]=phi[i]*(prime[j]-1); } } } inline int fpow(int a,int b,int mod){ int ans=1; bool ok=0; for(;b;b>>=1,a=a*a){ if(a>=mod)ok=1,a%=mod; if(b&1)ans=ans*a; if(ans>=mod)ok=1; ans%=mod; } return ans+ok*mod; } int dfs(int l,int r,int mod){ int w=query(l); if(mod==1||w==1) return 1; if(l==r) return w>=mod?w%mod+mod:w; return fpow(w,dfs(l+1,r,phi[mod]),mod); } inline void work(){ n=read(),m=read(); int pre=0,zp=0; for(int i=1;i<=n;i++)zp=read(),update(i,zp-pre),pre=zp; while(m--){ if(read()==1){ int l=read(),r=read(),k=read(); update(l,k),update(r+1,-k); }else{ int l=read(),r=read(),p=read(); printf("%lld\n",dfs(l,r,p)%p); } } } } signed main(){ star::pre(20000000); star::work(); return 0; }
|