QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#83#23584#3264. 相逢是问候QingyuQingyuSuccess!2022-03-17 22:29:022022-03-17 22:29:04

Details

Extra Test:

Wrong Answer
time: 7ms
memory: 15368kb

input:

1 4 68126603 34175647
9699309
0 1 1
1 1 1
0 1 1
1 1 1

output:

49498124
0

result:

wrong answer 2nd lines differ - expected: '1268001', found: '0'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#23584#3264. 相逢是问候Qingyu97 425ms27248kbC++203.3kb2022-03-17 22:25:572022-04-30 03:35:53

answer

// From Luogu: https://www.luogu.com.cn/blog/_post/89884
#include<cmath>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>

#define M 55
#define maxn 100005

using namespace std;

typedef unsigned long long LL;

template<typename T> inline void read(T &x){
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
}

int n,m,mint,ls[maxn],rs[maxn],ti[maxn],cnt;

LL p,c,pow1[maxn][M],pow2[maxn][M],v[maxn],val[maxn],phi[M];

bool flag,b1[maxn][M],b2[maxn][M];

inline LL calc_phi(LL vi){
    LL ret=vi,tmp=vi,lim=sqrt(vi);
    for(LL i=2;i<=lim;++i){
        if(!(tmp%i)){
            ret=ret*(i-1)/i;
            while(!(tmp%i)) tmp/=i;
        }
    }
    if(tmp>1) ret=ret*(tmp-1)/tmp;
    return ret;
}

inline void pre_work(){
    LL tmp=p; phi[0]=p;
    while(tmp!=1) tmp=calc_phi(tmp),phi[++mint]=tmp;
    phi[++mint]=1;
    for(int i=0;i<=mint;++i){
        pow1[0][i]=1;
        for(int j=1;j<=10000;++j){
            pow1[j][i]=pow1[j-1][i]*c;
            if(pow1[j][i]>=phi[i]) pow1[j][i]%=phi[i],b1[j][i]=1;
            b1[j][i]|=b1[j-1][i];
        }
    }
    for(int i=0;i<=mint;++i){
        pow2[0][i]=1;
        b2[1][i]=b1[10000][i];
        for(int j=1;j<=10000;++j){
            pow2[j][i]=pow2[j-1][i]*pow1[10000][i];
            if(pow2[j][i]>=phi[i]) pow2[j][i]%=phi[i],b2[j][i]=1;
            b2[j][i]|=b2[j-1][i];
        }
    }
}

inline LL calc(LL v,LL id){
    flag=0;
    LL v1=v%10000,v2=v/10000,ret=pow1[v1][id]*pow2[v2][id];
    if(ret>=phi[id]) ret=ret%phi[id],flag=1;
    flag|=b1[v1][id]|b2[v2][id];
    return ret;
}

inline LL dfs(LL vi,int deep,int lim){
    flag=0;
    if(deep==lim){
        if(vi>=phi[deep]) flag=1,vi%=phi[deep];
        return vi;
    }
    LL ci=dfs(vi,deep+1,lim);
    return calc(flag?ci+phi[deep+1]:ci,deep);
}

inline void build(int L,int R,int cur){
    if(L==R){ read(v[cur]); val[cur]=v[cur]; return ; }
    int mid=(L+R)>>1;
    ls[cur]=++cnt; build(L,mid,ls[cur]);
    rs[cur]=++cnt; build(mid+1,R,rs[cur]);
    val[cur]=val[ls[cur]]+val[rs[cur]];
    if(val[cur]>=p) val[cur]-=p;
}

inline void update(int L,int R,int l,int r,int cur){
    if(ti[cur]>=mint) return ;
    if(L==R){
        ++ti[cur]; val[cur]=dfs(v[cur],0,ti[cur]);
        return ;
    }
    int mid=(L+R)>>1;
    if((l<=mid)&&(ti[ls[cur]]<mint)) update(L,mid,l,r,ls[cur]);
    if((r>mid)&&(ti[rs[cur]]<mint)) update(mid+1,R,l,r,rs[cur]);
    val[cur]=val[ls[cur]]+val[rs[cur]];
    if(val[cur]>=p) val[cur]-=p;
    ti[cur]=min(ti[ls[cur]],ti[rs[cur]]);
}

inline LL query(int L,int R,int l,int r,int cur){
    if((l<=L)&&(R<=r)) return val[cur];
    int mid=(L+R)>>1; LL ret=0;
    if(l<=mid) ret=query(L,mid,l,r,ls[cur]);
    if(r>mid) ret+=query(mid+1,R,l,r,rs[cur]);
    return (ret>=p)?(ret-p):ret;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    read(n); read(m); read(p); read(c);
    build(1,n,++cnt); pre_work();
    while(m--){
        int opt,l,r;
        read(opt); read(l); read(r);
        if(opt==0) update(1,n,l,r,1);
        else if(opt==1) cout<<query(1,n,l,r,1)<<endl;
    }
    return 0;
}