跳到正文

剪贴板

剪贴板 120ywqjx

原文
剪贴板信息

剪贴板信息

收录
更新

xxy W4 o0 井1O 的代码被我调好了

C++
#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long
#define lowbit(x) ((x)&(-x))
const int maxn = 5e5+5,maxp = 2e7+5;
int tree[maxn],a[maxn],phi[maxp];
bool vis[maxp];
int pr[maxn];
int n,m;
void update(int x,int k){
	while(x<=n)tree[x]+=k,x+=lowbit(x);
}
int query(int x){
	int res = 0;
	while(x)res+=tree[x],x-=lowbit(x);
	return res;
}
struct node{
	int v;
	int f;
};
node qpow(int a,int b,int mod){
	node ans = {1,0};
    if(a==0){
        return {0,1};
    }
	if(a>=mod){
		ans.f = 1;
		a%=mod;
	}
    while(b){
        if(b&1)ans.v = ans.v*a;
		if(ans.v>=mod){
			ans.f = 1;
			ans.v%=mod;
		}
        a = a*a;
		if(a>=mod){
			ans.f = 1;
			a%=mod;
		}
        b/=2;
    }
    return ans;
}
inline void init(){
    int cnt = 0;
    phi[1] = 1;
    for(register int i=2;i<=20000000;++i){
        if(!vis[i]){
            pr[++cnt] = i;
            phi[i] = i-1;
        }
        for(register int j=1;j<=cnt;++j){
            int t = i*pr[j];
            if(t>20000000) break;
            vis[t] = 1;
            if(i%pr[j]==0){
                phi[t] = phi[i]*pr[j];
                break;
            }
            phi[t] = phi[i]*(pr[j]-1);
        }
    }
}
node solve(int l,int r,int p){
    if(p==1) return {0,1};
    int val = query(l)%p;
    if(val==0) return {0,1}; 
    if(val==1) return {1,query(l)>=p}; 
    if(l==r) return {val,query(l)>=p};
    node ans = solve(l+1,r,phi[p]);
    if(ans.f) ans.v+=phi[p];
    return qpow(val,ans.v,p);
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0); 
	cin>>n>>m;
	init();
	for(int i = 1;i<=n;i++){
		cin>>a[i];
		update(i,a[i]-a[i-1]);
	}
	int op,x,y,k;
	while(m--){
		cin>>op;
		if(op == 1){
			cin>>x>>y>>k;
			update(x,k);
			update(y+1,-k);
		}else{
			cin>>x>>y>>k;
			cout<<solve(x,y,k).v<<'\n';
		}
	}
	return 0;
}