REQ CodeForces - 594D

题目链接:传送门

题目:

Today on a math lesson the teacher told Vovochka that the Euler function of a positive integer φ(n) is an arithmetic function that counts the positive integers less than or equal to n that are relatively prime to n. The number 1 is coprime to all the positive integers and φ(1) = 1.

Now the teacher gave Vovochka an array of n positive integers a1, a2, ..., an and a task to process q queries li ri — to calculate and print  modulo 109 + 7. As it is too hard for a second grade school student, you've decided to help Vovochka.

Input

The first line of the input contains number n (1 ≤ n ≤ 200 000) — the length of the array given to Vovochka. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106).

The third line contains integer q (1 ≤ q ≤ 200 000) — the number of queries. Next qlines contain the queries, one per line. Each query is defined by the boundaries of the segment li and ri (1 ≤ li ≤ ri ≤ n).

Output

Print q numbers — the value of the Euler function for each query, calculated modulo 109 + 7.

Examples

Input

10
1 2 3 4 5 6 7 8 9 10
7
1 1
3 8
5 6
4 8
8 10
7 9
7 10

Output

1
4608
8
1536
192
144
1152

Input

7
24 63 13 52 6 10 1
6
3 5
4 7
1 7
2 4
3 6
2 6

Output

1248
768
12939264
11232
9984
539136

Note

In the second sample the values are calculated like that:

  • φ(13·52·6) = φ(4056) = 1248
  • φ(52·6·10·1) = φ(3120) = 768
  • φ(24·63·13·52·6·10·1) = φ(61326720) = 12939264
  • φ(63·13·52) = φ(42588) = 11232
  • φ(13·52·6·10) = φ(40560) = 9984
  • φ(63·13·52·6·10) = φ(2555280) = 539136

题目大意:给你n个数,m次询问,每次询问给你一个l到r,让你求区间l到r乘积的欧拉函数;

思路:由欧拉函数公式 φ(x)=x(1-1/p(1))(1-1/p(2))(1-1/p(3))(1-1/p(4))…..(1-1/p(n)) 其中p(1),p(2)…p(n)为x

的所有质因数;x是正整数; φ(1)=1(唯一和1互质的数,且小于等于1)。可以预处理出来每一个数的质因子储存,区间乘积的欧拉函数就可以由前缀积与区间里面出现的质因子对应公式算出,那现在的问题就是维护区间的质数乘积,质数·可以由树状数组维护前缀积,对查询进行离线处理,每次数组数组处理后只会对前面产生影响,每次出现对对应位置新出现的的质数加入树状数组进行维护,对询问按端点进行排序后,根据询问结果储存,按询问顺序输出即可;

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<cstdlib>
using namespace std;
const int maxn=1e6+10,maxn1=2e5+10,mod=1e9+7;
struct node{
    int l,r,id;
};
vector<int> v[maxn];
int x1[maxn1];
long long x2[maxn1],ans[maxn1];
long long tree[maxn1];
long long y[maxn],y1[maxn];
node qu[maxn1];
int cmp(node a,node b){
    if(a.r<b.r)
        return 1;
    return 0;
}
void y2(){
    memset(y,-1,sizeof(y));
    y[1]=0;
    for(int a=2;a<=maxn-10;a++){
        if(y[a]==-1){
            v[a].push_back(a);
            y[a]=0;
            for(int b=a+a;b<=maxn-10;b+=a){
                v[b].push_back(a);
                y[b]=0;
            }
        }
    }
    for(int a=1;a<=maxn1-10;a++)
        tree[a]=1;
}
long long get1(long long x){
    long long sum1=x,sum2=1;
    int n=mod-2;
    while(1){
        if(n&1!=0){
            n--;
            sum2*=sum1;
            sum2%=mod;
        }
        n/=2;
        sum1*=sum1;
        sum1%=mod;
        if(n==1)
            break;
    }
    sum1=sum1*sum2;
    sum1%=mod;
    return sum1;
}
int lowbit(int x){
    return x&(-x);
}
void add(int x,long long k){
    while(x<=maxn1-10){
        tree[x]*=k;
        tree[x]%=mod;
        x+=lowbit(x);
    }
}
long long getsum(int x){
    if(x==0)
        return 1;
    long long sum=1;
    while(x>0){
        sum*=tree[x];
        sum%=mod;
        x-=lowbit(x);
    }
    return sum;
}
int main( ){
    y2();
    int n;
    scanf("%d",&n);
    x2[0]=1;
    for(int a=1;a<=n;a++){
        scanf("%d",&x1[a]);
        x2[a]=x2[a-1]*x1[a]%mod;
    }
    int q;
    scanf("%d",&q);
    for(int a=1;a<=q;a++){
        scanf("%d %d",&qu[a].l,&qu[a].r);
        qu[a].id=a;
    }
    sort(qu+1,qu+q+1,cmp);
    int l1=1;
    long long sum1=1;
    for(int a=1;a<=n;a++){

        int len=v[x1[a]].size();
        for(int b=0;b<len;b++){

            int c=v[x1[a]][b];
            if(y[c]==0){
                 y1[c]=(c-1)*get1(c)%mod;
                 add(a,y1[c]);
                 y[c]=a;
                 sum1*=y1[c];
                 sum1%=mod;
            }
            else{
                add(y[c],get1(y1[c]));
                add(a,y1[c]);
                y[c]=a;
            }
        }
        for(;l1<=q;l1++){
            if(qu[l1].r==a){

                ans[qu[l1].id]=x2[qu[l1].r]*get1(x2[qu[l1].l-1])%mod*sum1%mod*get1(getsum(qu[l1].l-1))%mod;
            }
            else
                break;
        }
    }
    for(int a=1;a<=q;a++){
        printf("%lld\n",ans[a]);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值