【Codeforces 1114 F. Please, another Queries on Array?】 线段树维护区间乘积 区间欧拉函数

Codeforces 1114F
题意 维护区间乘 求区间乘积的欧拉函数
ϕ ( n )    =    n Π p r i m e ∣ n p r i m e −    1 p r i m e 那么对于我们区间乘来说 因为小于300内的质数很少 所以我们可以用 l o n g    l o n g    维护 然后用线段树维护一个区间乘积  只不过区间乘积需要快速幂优化 \phi \left( n \right) \,\,=\,\,n\underset{prime|n}{\varPi}\frac{prime-\,\,1}{prime}\text{那么对于我们区间乘来说 因为小于300内的质数很少} \\ \text{所以我们可以用}long\,\,long\,\,\text{维护 然后用线段树维护一个区间乘积 } \\ \text{只不过区间乘积需要快速幂优化} \\ ϕ(n)=nprimenΠprimeprime1那么对于我们区间乘来说 因为小于300内的质数很少所以我们可以用longlong维护 然后用线段树维护一个区间乘积 只不过区间乘积需要快速幂优化

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<#define dbg3(x1,x2,x3) cout<<#x1<<" = "<#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define MOD 1000000007

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 4e5+5,M = 301,Lim = 62;
//inv[1]=1;
//for(int i=2; i
//for(int i=0; i
//2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293
//0,0,0,1,0,2,0,3,0,0,0,4,0,5,0,0,0,6,0,7,0,0,0,8,0,0,0,0,0,9,0,10,0,0,0,0,0,11,0,0,0,12,0,13,0,0,0,14,0,0,0,0,0,15,0,0,0,0,0,16,0,17,0,0,0,0,0,18,0,0,0,19,0,20,0,0,0,0,0,21,0,0,0,22,0,0,0,0,0,23,0,0,0,0,0,0,0,24,0,0,0,25,0,26,0,0,0,27,0,28,0,0,0,29,0,0,0,0,0,0,0,0,0,0,0,0,0,30,0,0,0,31,0,0,0,0,0,32,0,33,0,0,0,0,0,0,0,0,0,34,0,35,0,0,0,0,0,36,0,0,0,0,0,37,0,0,0,38,0,0,0,0,0,39,0,0,0,0,0,40,0,41,0,0,0,0,0,0,0,0,0,42,0,43,0,0,0,44,0,45,0,0,0,0,0,0,0,0,0,0,0,46,0,0,0,0,0,0,0,0,0,0,0,47,0,0,0,48,0,49,0,0,0,50,0,0,0,0,0,51,0,52,0,0,0,0,0,0,0,0,0,53,0,0,0,0,0,54,0,0,0,0,0,55,0,0,0,0,0,56,0,57,0,0,0,0,0,58,0,0,0,59,0,60,0,0,0,0,0,0,0,0,0,61,0,0,0,0,0,0,0
int P[Lim] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293};
int rev[M] ={0,0,0,1,0,2,0,3,0,0,0,4,0,5,0,0,0,6,0,7,0,0,0,8,0,0,0,0,0,9,0,10,0,0,0,0,0,11,0,0,0,12,0,13,0,0,0,14,0,0,0,0,0,15,0,0,0,0,0,16,0,17,0,0,0,0,0,18,0,0,0,19,0,20,0,0,0,0,0,21,0,0,0,22,0,0,0,0,0,23,0,0,0,0,0,0,0,24,0,0,0,25,0,26,0,0,0,27,0,28,0,0,0,29,0,0,0,0,0,0,0,0,0,0,0,0,0,30,0,0,0,31,0,0,0,0,0,32,0,33,0,0,0,0,0,0,0,0,0,34,0,35,0,0,0,0,0,36,0,0,0,0,0,37,0,0,0,38,0,0,0,0,0,39,0,0,0,0,0,40,0,41,0,0,0,0,0,0,0,0,0,42,0,43,0,0,0,44,0,45,0,0,0,0,0,0,0,0,0,0,0,46,0,0,0,0,0,0,0,0,0,0,0,47,0,0,0,48,0,49,0,0,0,50,0,0,0,0,0,51,0,52,0,0,0,0,0,0,0,0,0,53,0,0,0,0,0,54,0,0,0,0,0,55,0,0,0,0,0,56,0,57,0,0,0,0,0,58,0,0,0,59,0,60,0,0,0,0,0,0,0,0,0,61,0,0,0,0,0,0,0};
long long val[MAX_N<<2],col[MAX_N<<2];
long long now,Ans,s[MAX_N<<2],colf[MAX_N<<2];
void up(int rt,int l,int r)
{
    s[rt] = s[rt<<1]|s[rt<<1|1];
    val[rt] = 1ll*val[rt<<1]*val[rt<<1|1]%MOD;
}
long long FP(int x,int k)
{
    long long t = 1;
    for(;k;k>>=1,x=1ll*x*x%MOD)
        if(k&1) t = 1ll*t *x %MOD;
    return t;
}
void update(int rt,int l,int r,long long v,long long vf)
{
    val[rt] = 1ll*val[rt] *FP(v,r-l+1)%MOD;
    col[rt] = 1ll*col[rt]*v%MOD;
    s[rt]|=vf;
    colf[rt]|=vf;
}

void build(int rt,int l,int r)
{
    col[rt] = 1;
    if(l==r)
    {
        int v;
        scanf("%d",&v);
        val[rt] = v;
        for(int i = 0;i<Lim&&P[i]*P[i]<=v;++i)
        {
            if(v%P[i]==0)
            {
                s[rt] |= 1ll<<i,v/=P[i];
                while(v%P[i]==0) v/=P[i];
            }
        }
        if(v!=1) s[rt]|=1ll<<rev[v];
        return ;
    }
    int mid = (l+r)>>1;
    build(rt<<1,l,mid),build(rt<<1|1,mid+1,r);
    up(rt,l,r);
}
void down(int rt,int l,int r)
{
    int mid =  (l+r)>>1;
    update(rt<<1,l,mid,col[rt],colf[rt]);
    update(rt<<1|1,mid+1,r,col[rt],colf[rt]);
    col[rt] = 1;
    colf[rt] = 0;
}
void modify(int rt,int l,int r,int x,int y,long long v)
{
    if(x<=l&&r<=y)
    {
        update(rt,l,r,v,now);
        return ;
    }
    if(col[rt]!=1) down(rt,l,r);
    int mid = (l+r)>>1;
    if(x<=mid) modify(rt<<1,l,mid,x,y,v);
    if(mid<y) modify(rt<<1|1,mid+1,r,x,y,v);
    up(rt,l,r);
}
long long query(int rt,int l,int r,int x,int y)
{
    if(x<=l&&r<=y) return Ans|=s[rt],val[rt];
    if(col[rt]!=1) down(rt,l,r);
    int mid = (l+r)>>1;
    if(x<=mid)
        if(mid<y) return 1ll*query(rt<<1,l,mid,x,y)*query(rt<<1|1,mid+1,r,x,y)%MOD;
        else return query(rt<<1,l,mid,x,y);
    return query(rt<<1|1,mid+1,r,x,y);
}
int inv[M],coef[M];
char str[15];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    inv[1] = 1;
    for(int i = 2;i<M;++i) inv[i] = 1ll*(MOD-MOD/i)*inv[MOD%i]%MOD;
    for(int i = 0;i<Lim;i++) coef[i] = 1ll*(P[i]-1)*inv[P[i]]%MOD;
    int n,q,x,y;
    scanf("%d%d",&n,&q);
    build(1,1,n);
    while(q--)
    {
        scanf("%s",str);
        if(str[0]=='T')
        {
            Ans = 0;
            scanf("%d%d",&x,&y);
            long long VAL = query(1,1,n,x,y);
            for(int i = 0;i<Lim;++i)
                if(Ans>>i&1) VAL = 1ll*VAL*coef[i]%MOD;
            printf("%lld\n",VAL);
        }
        else
        {
            int V,tmp;
            scanf("%d%d%d",&x,&y,&tmp);
            V=  tmp;
            now = 0;
            for(int i = 0;i<Lim&&P[i]*P[i]<=V;++i)
            {
                if(V%P[i]==0)
                {
                    now|=1<<i,V/=P[i];
                    while(V%P[i]==0) V/=P[i];
                }
            }
            if(V!=1) now|=1ll<<rev[V];
            //dbg(now);
            modify(1,1,n,x,y,tmp);
        }
    }
    //printf("\n\n%d\n",tmp);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

你可能感兴趣的:(ACM,DS-线段树)