题目
给定 \(n\) 个数,求 \(\varphi(\prod_{i=l}^r{a_i})\)
分析
考虑单个欧拉函数的求法,只需要求出这个数的质因数计算即可。
那么考虑离线,枚举右端点,记录每个质因数的最晚出现位置,
那么在上一位置乘上 \(\frac{p}{p-1}\),在当前位置乘上 \(\frac{p-1}{p}\),树状数组维护即可
代码
#include <cstdio>
#include <cctype>
#define rr register
using namespace std;
const int N=200011,M=1000000,mod=1000000007; struct rec{int y,next;}e[N];
int c[N],inv[N*5],prime[N],v[N*5],Fac[N],Inv[N],Cnt,a[N],las[N*5],n,Q,as[N],ans[N];
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
inline void print(int ans){
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
inline signed ksm(int x,int y){
rr int ans=1;
for (;y;y>>=1,x=1ll*x*x%mod)
if (y&1) ans=1ll*ans*x%mod;
return ans;
}
inline void update(int x,int y){
for (;x<=n;x+=-x&x) c[x]=1ll*c[x]*y%mod;
}
inline signed query(int x){
rr int ans=1;
for (;x;x-=-x&x) ans=1ll*ans*c[x]%mod;
return ans;
}
signed main(){
n=iut(),inv[0]=Fac[0]=inv[1]=v[1]=1;
for (rr int i=2;i<=M;++i){
inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
if (!v[i]) prime[++Cnt]=v[i]=i;
for (rr int j=1;j<=Cnt&&prime[j]<=M/i;++j){
v[i*prime[j]]=prime[j];
if (i%prime[j]==0) break;
}
}
for (rr int i=1;i<=n;++i) a[i]=iut(),Fac[i]=1ll*Fac[i-1]*a[i]%mod;
Inv[n]=ksm(Fac[n],mod-2),Q=iut();
for (rr int i=n;i;--i) Inv[i-1]=1ll*Inv[i]*a[i]%mod,c[i]=1;
for (rr int i=1;i<=Q;++i){
rr int l=iut(),r=iut();
e[i]=(rec){l-1,as[r]},as[r]=i;
}
for (rr int i=1;i<=n;++i){
while (a[i]>1){
rr int now=v[a[i]];
if (las[now]) update(las[now],inv[now-1]+1);
update(i,mod-inv[now]+1),las[now]=i;
while (a[i]%now==0) a[i]/=now;
}
rr int t=1ll*Fac[i]*query(i)%mod;
for (rr int j=as[i];j;j=e[j].next)
if (e[j].y) ans[j]=1ll*Inv[e[j].y]*t%mod*ksm(query(e[j].y),mod-2)%mod;
else ans[j]=t;
}
for (rr int i=1;i<=Q;++i) print(ans[i]),putchar(10);
return 0;
}