题解 P6217 【简单数论题】
好题!!!!(比我出的好多了)(比赛时sb写了个
求
做题做多了,就会发现,
而上面的东西明显可以预处理。
现在就转为求
这个东西似乎不好反演?(反正我不会)
考虑枚举每一个
我们发现,如果将每一个数分解质因数,那么
也就是说:
假设一个数
那么贡献为
其中
由于
可以将所有的
可以提前预处理出所有质数(线性筛),来降低分解质因数复杂度。
总复杂度:
时间复杂度
如果你嫌这个做法慢,Pollard-Rho 了解一下
#include<cstdio>
#include<cstring>
#include<cmath>
#define re register
#define M 1000000007
using namespace std;
int a[200002],n,p[200002],b[200002],siz[80000002],tot,ls[80000002],rs[80000002],rt[200002],cnt,pos[1000002];
bool ip[200012];
const int Mxdt=200000;
inline char gc(){
static char buf[Mxdt],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxdt,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
re int t=0;
re char v=gc();
while(v<'0')v=gc();
while(v>='0') {
t=(t<<3)+(t<<1)+v-48;
v=gc();
}
return t;
}
inline void insert(re int &x,re int y,re int l,re int r,re int v){
bool nw=0;
if(!x){
x=++cnt;nw=1;
siz[x]=siz[y]+1; }
else ++siz[x];
if(l==r){return;}
re int mid=l+r>>1;
if(v<=mid){
if(nw)rs[x]=rs[y];
insert(ls[x],ls[y],l,mid,v);
}
else{
if(nw)ls[x]=ls[y];
insert(rs[x],rs[y],mid+1,r,v);
}
}
inline int count(re int x,re int l,re int r,re int v){
if(!x)return 0;
if(l==r){return siz[x];}
re int mid=l+r>>1;
if(v<=mid)return count(ls[x],l,mid,v);
else return count(rs[x],mid+1,r,v);
}
inline int gcd(re int x,re int y){
return y?gcd(y,x%y):x;
}
inline int ksm(re int x,re int y){
re int ans=1;
while(y){
if(y&1)ans=1ll*ans*x%M;
x=1ll*x*x%M;
y>>=1;
}
return ans;
}
inline int max(re int x,re int y) {
return x>y?x:y;
}
int l,r,q,x;
int main() {
n=read();
q=read();
for (int i=2; i<=200010; i++) {
if (!ip[i]) {
p[++tot]=i;
pos[i]=tot;
}
for (int j=1; j<=tot&&1ll*i*p[j]<=200010; j++) {
ip[i*p[j]]=1;
if (i%p[j]==0)break;
}
}
a[0]=1;
for(re int i=1; i<=n; ++i) {
re int x=b[i]=read(),num=0;
a[i]=1ll*a[i-1]*x%M;
re int nw=0,lst=rt[i-1];
re int k=sqrt(x);
for(re int j=1;p[j]<=k;++j){
num=1;
while(x%p[j]==0)x/=p[j],num*=p[j],insert(nw,lst,1,200000,num),lst=nw,nw=0;
}
if(x!=1)insert(nw,lst,1,200000,x),lst=nw,nw=0;
if(b[i]==1){
rt[i]=rt[i-1];
}
else rt[i]=lst;
}
while(q--){
l=read();r=read();re int x1=x=read(),ans=1;
re int k=sqrt(x);
for(re int i=1;p[i]<=k;++i){
re int num=1;
while(x1%p[i]==0){
x1/=p[i];
num*=p[i];
ans=1ll*ans*ksm(p[i],(count(rt[r],1,200000,num)-count(rt[l-1],1,200000,num)))%M;
}
}
if(x1!=1){
ans=1ll*ans*ksm(x1,(count(rt[r],1,200000,x1)-count(rt[l-1],1,200000,x1)))%M;
}
printf("%d\n",1ll*a[r]*ksm(a[l-1],M-2)%M*ksm(x,r-l+1)%M*ksm(ans,M-2)%M);
}
}