牛客 40F 珂朵莉的约数 (莫队)

珂朵莉给你一个长为n的序列,有m次查询 

每次查询给两个数l,r

设s为区间[l,r]内所有数的乘积 

求s的约数个数mod 1000000007

直接莫队暴力维护复杂度是$O(8m\sqrt{m})$.

看了官方题解, 序列权值比较小, 权值<1000的素数暴力维护, >1000的素数最多只有1个, 用莫队维护, 这样能优化掉8的常数.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+10;
int n, m, sqn;
int gpf[N], ans[N], blo[N];
int inv[N];
vector<pii> a[N];
struct _ {
    int l,r,id;
    bool operator < (const _ & rhs) const {
         return blo[l]^blo[rhs.l]?l<rhs.l:blo[l]&1?r<rhs.r:r>rhs.r;
    }
} e[N];

int cnt[N], now=1;
void upd(int x, int v) {
	for (auto &&t:a[x]) {
		now = (ll)now*inv[cnt[t.x]+1]%P;
		cnt[t.x] += v*t.y;
		now = (ll)now*(cnt[t.x]+1)%P;
	}
}

int main() {
	scanf("%d%d", &n, &m),sqn=pow(n,0.55);
	inv[1] = 1;
	REP(i,2,N-1) inv[i]=(ll)inv[P%i]*(P-P/i)%P;
	gpf[1] = 1;
	REP(i,1,N-1) if (!gpf[i]) for (int j=i;j<N;j+=i) gpf[j]=i;
	REP(i,1,n) { 
		int t;
		scanf("%d", &t);
		while (t!=1) {
			int x = gpf[t], y = 0;
			while (t%x==0) t/=x,++y;
			a[i].pb(pii(x,y));
		}
		blo[i]=i/sqn;
	}
	REP(i,1,m) scanf("%d%d",&e[i].l,&e[i].r),e[i].id=i;
	sort(e+1,e+1+m);
	int ql=1, qr=0;
	REP(i,1,m) {
		while (ql>e[i].l) upd(--ql,1);
		while (qr<e[i].r) upd(++qr,1);
		while (ql<e[i].l) upd(ql++,-1);
		while (qr>e[i].r) upd(qr--,-1);
		ans[e[i].id] = now;
	}
	REP(i,1,m) printf("%d\n",ans[i]);
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/10920576.html