P3747 [six provinces exam 2017] reunion greetings

The meaning of problems

If one of the operands \ (K \) times, then the number becomes \ (C ^ C ^ {{...}}} ^ {a_i \) , where \ (C \) has \ (K \) a.

According to proper usage P4139 God and the collection of this question, we can see a number of changes to their continued Euler function, about \ (log \) times would be over, and any number of analog \ (1 \) are \ (0 \) , so we can solve the potential tree line.

Because the modulus of the same, so we can pre-all \ (\ varphi (\ varphi (... \ varphi (the p-) ...)) \) , the segment tree after recording the number of operations.

This is three \ (log \) , because even the fast power, can each \ (\ varphi (\ varphi ( ... \ varphi (p) ...)) \) power preprocessing, with the speed of light solve.

Note that the extension Chinese remainder theorem \ (a_k \ equiv a ^ { k \% \ varphi (p) + \ varphi (p)} \ pmod {p} \) applies if and only if \ (k \ geqslant \ varphi ( p ) \) , so we use when evaluating a \ (flag \) Indicates if you want \ (+ \ varphi (the p-) \) .

code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
const int maxn=50010;
const int maxt=10010;
int n,m,mod,C,maxtim;
int a[maxn];
int pw1[60][maxt],pw2[60][maxt];
bool flag;
bool flag1[60][maxt],flag2[60][maxt];
vector<int>ve;
struct Seg
{
    #define sum(p) (seg[p].sum)
    #define cnt(p) (seg[p].cnt)
    int sum,cnt;
}seg[maxn<<2];
inline int read()
{
    char c=getchar();int res=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')res=res*10+c-'0',c=getchar();
    return res*f;
}
inline int phi(int x)
{
    int res=x,tmp=x;
    for(int i=2;i*i<=tmp;i++)
    {
        if(tmp%i)continue;
        res=res/i*(i-1);
        while(tmp%i==0)tmp/=i;
    }
    if(tmp>1)res=res/tmp*(tmp-1);
    return res;
}
inline void pre_work()
{
    int tmp=mod;
    ve.push_back(mod);
    while(tmp>1)tmp=phi(tmp),ve.push_back(tmp);
    ve.push_back(1);
    for(unsigned int i=0;i<ve.size();i++)
    {
        pw1[i][0]=1;
        for(int j=1;j<=10000;j++)
        {
            pw1[i][j]=pw1[i][j-1]*C;
            if(pw1[i][j]>=ve[i])pw1[i][j]%=ve[i],flag1[i][j]=1;
            flag1[i][j]|=flag1[i][j-1];
        }
    }
    for(unsigned int i=0;i<ve.size();i++)
    {
        pw2[i][0]=1;flag2[i][1]=flag1[i][10000];
        for(int j=1;j<=10000;j++)
        {
            pw2[i][j]=pw2[i][j-1]*pw1[i][10000];
            if(pw2[i][j]>=ve[i])pw2[i][j]%=ve[i],flag2[i][j]=1;
            flag2[i][j]|=flag2[i][j-1];
        }
    }
}
inline void up(int p)
{
    sum(p)=(sum(ls(p))+sum(rs(p)))%mod;
    cnt(p)=min(cnt(ls(p)),cnt(rs(p)));
}
void build(int p,int l,int r)
{
    if(l==r){sum(p)=a[l];return;}
    int mid=(l+r)>>1;
    build(ls(p),l,mid);build(rs(p),mid+1,r);
    up(p);
}
inline int power(int x,int id)
{
    flag=0;
    int res=pw1[id][x%10000]*pw2[id][x/10000];
    if(res>=ve[id])res%=ve[id],flag=1;
    flag|=flag1[id][x%10000]|flag2[id][x/10000];
    return res;
}
int calc(int x,int dep,int k)
{
    flag=0;
    if(dep==k)
    {
        if(x>=ve[dep])flag=1,x%=ve[dep];
        return x;
    }
    int tmp=calc(x,dep+1,k);
    return power(flag?tmp+ve[dep+1]:tmp,dep);
}
void change(int p,int l,int r,int ql,int qr)
{
    if(cnt(p)>=(int)ve.size()-1)return;
    if(l==r)
    {
        cnt(p)++;
        sum(p)=calc(a[l],0,cnt(p));
        return;
    }
    int mid=(l+r)>>1;
    if(ql<=mid)change(ls(p),l,mid,ql,qr);
    if(qr>mid)change(rs(p),mid+1,r,ql,qr);
    up(p);
}
int query(int p,int l,int r,int ql,int qr)
{
    if(l>=ql&&r<=qr)return sum(p);
    int mid=(l+r)>>1,res=0;
    if(ql<=mid)res=(res+query(ls(p),l,mid,ql,qr))%mod;
    if(qr>mid)res=(res+query(rs(p),mid+1,r,ql,qr))%mod;
    return res;
}
signed main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout); 
    n=read();m=read();mod=read();C=read();
    for(int i=1;i<=n;i++)a[i]=read();
    build(1,1,n);
    pre_work();
    for(int i=1;i<=m;i++)
    {
        int op=read(),l=read(),r=read();
        if(!op)change(1,1,n,l,r);
        else printf("%lld\n",query(1,1,n,l,r)); 
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/nofind/p/12078423.html