bzoj 3930: [CQOI2015] Selection [fast power + tolerance and exclusion]

Reference: https://www.cnblogs.com/iwtwiioi/p/4986316.html
Note that the interval length is 1e5 level.
Assuming that the n numbers are not all the same, then their gcd is less than the maximum number-minimum number, which proves that: then gcdk2−gcdk1=gcd(k2−k1)>d
, so we will make a special judgment on the case where they are all equal,
and then divide the interval by k , so the problem turns into finding gcd==1, and let f[i] be the number of solutions where gcd is i. Enumerate the divisors from large to small, select the selection situation by fast exponentiation calculation, and then subtract the f of the multiple of the constraint (tolerance and exclusion)

#include<iostream>
#include<cstdio>
using namespace std;
const int N=100005,mod=1e9+7;
int n,k,l,r,len,p,f[N];
int ksm(int a,int b)
{
    int r=1;
    while(b)
    {
        if(b&1)
            r=1ll*r*a%mod;
        a=1ll*a*a%mod;
        b>>=1;
    }
    return r;
}
int main()
{
    scanf("%d%d%d%d",&n,&k,&l,&r);
    if(l<=k&&r>=k)
        p=1;
    l=(l-1)/k,r=r/k,len=r-l;
    for(int i=len;i>=1;i--)
    {
        int x=l/i,y=r/i;
        f[i]=(ksm(y-x,n)-y+x+mod)%mod;
        for(int j=i*2;j<=len;j+=i)
            f[i]=((f[i]-f[j])%mod+mod)%mod;
    }
    printf("%d\n",f[1]+p);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324856027&siteId=291194637
Recommended