bzoj3930 [CQOI2015] Selection (Inclusion and Exclusion + Recursion + Number Theory)

First of all we have: select a number of different numbers in [l, R], their gcd<=RL. Proof can be rebutted.
We set f[i] to denote the number of gcd exactly K*i. (Choose several numbers that are not exactly the same), consider the calculation of tolerance and exclusion, and push it backwards.
f[i]=(RL)^n-(RL)-f[i*j] (R=r/(ki), L=(l-1)/(ki))
Finally, we will judge whether it can be completely the same K.
the complexity O ( n l o gn)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
#define mod 1000000007
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int f[N];//f[i]--gcd==i*k的个数
inline int ksm(int x,int k){
    int res=1;for(;k;k>>=1,x=(ll)x*x%mod) if(k&1) res=(ll)res*x%mod;return res;
}
int main(){
//  freopen("a.in","r",stdin);
    int n=read(),K=read(),L=read(),R=read(),lim=(R-L)/K;
    for(int i=lim;i>=1;--i){
        int l=(L-1)/(K*i),r=R/(K*i);
        f[i]=ksm(r-l,n)-(r-l);f[i]%=mod;
        for(int j=2;i*j<=lim;++j) f[i]-=f[i*j],f[i]%=mod;
        if(f[i]<0) f[i]+=mod;
    }if(L<=K&&K<=R) f[1]++;
    printf("%d\n",f[1]%mod);
    return 0;
}

Guess you like

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