51nod1203 JZPLCM rmq+Moq

Posted by mikeyca on Fri, 31 Jan 2020 21:52:55 +0100

Description

A positive integer sequence S with a length of N is asked Q times, each time LCM (that is, the least common multiple) of all the numbers in an interval.Since the answer may be large, output the answer Mod 10^9 + 7.
For example: 234 5, ask [1,3] interval for the least common multiple = 12, which is 234.

2 <= N, Q <= 50000
1 <= S[i] <= 50000

Solution

At first very sb thought it was sb writing a block, and then found that orz had to be modeled

Notice that the nature of lcm is that for a prime number p, the maximum number of times it occurs in a number within an interval is its exponent in lcm
And then we can break down the spicy prime factor
There are only about 50 prime numbers of < n_n, and the maximum of the interval index is maintained by rmq.
Each prime number (> n> n) contributes only once, and it is exactly itself.Number of occurrences in the maintenance interval with Moquet+Bucket
This kind of thing seems to be called balanced planning?

n is small, can preprocess multiplication inverse run faster

Code

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))

typedef long long LL;
const int MOD=1000000007;
const int N=100005;

struct Q {int l,r,id;} q[N];

int prime[N],a[N];
int mx[N][20],b[N];
int bct[N],B;
LL ans[N],ny[N],now=1;

bool not_prime[N+5];

int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

void pre_work(int n) {
    rep(i,2,n) {
        if (!not_prime[i]) prime[++prime[0]]=i;
        for (int j=1;i*prime[j]<=n&&j<=prime[0];j++) {
            not_prime[i*prime[j]]=1;
            if (i%prime[j]==0) break;
        }
    }
}

LL ksm(LL x,int dep) {
    LL ret=1;
    while (dep) {
        if (dep&1) ret=ret*x%MOD;
        x=x*x%MOD; dep/=2;
    }
    return ret;
}

int query(int l,int r) {
    int lg=floor(log2(r-l+1));
    return std:: max(mx[l][lg],mx[r-(1<<lg)+1][lg]);
}

void modify(int x,int v) {
    if (v==1) {
        if (++bct[x]==1) now=now*x%MOD;
    } else {
        if (--bct[x]==0) now=now*ny[x]%MOD;
    }
}

bool cmp(Q a,Q b) {
    return (a.l/B)<(b.l/B)||((a.l/B)==(b.l/B))&&(a.r<b.r);
}

int main(void) {
    freopen("data.in","r",stdin);
    rep(i,1,N-1) ny[i]=ksm(i,MOD-2);
    query(1,2);
    pre_work(230);
    int n=read(),m=read(); B=sqrt(n);
    rep(i,1,n) a[i]=read();
    rep(i,1,m) q[i]=(Q) {read(),read(),i},ans[i]=1;
    rep(j,1,prime[0]) {
        rep(i,1,n) {
            b[i]=0;
            if (a[i]%prime[j]==0) {
                for (;a[i]%prime[j]==0;b[i]++) a[i]/=prime[j];
            }
            mx[i][0]=b[i];
        }
        rep(k,1,17) rep(i,1,n) {
            mx[i][k]=std:: max(mx[i][k-1],mx[i+(1<<k-1)][k-1]);
        }
        rep(i,1,m) {
            int ret=query(q[i].l,q[i].r);
            ans[i]=ans[i]*ksm(prime[j],ret)%MOD;
        }
    }
    std:: sort(q+1,q+m+1,cmp);
    for (int i=1,l=1,r=0;i<=n;i++) {
        for (;r<q[i].r;) modify(a[++r],1);
        for (;r>q[i].r;) modify(a[r--],-1);
        for (;l>q[i].l;) modify(a[--l],1);
        for (;l<q[i].l;) modify(a[l++],-1);
        ans[q[i].id]=ans[q[i].id]*now%MOD;
    }
    rep(i,1,m) printf("%lld\n", ans[i]);
    return 0;
}