题解 P3172 【[CQOI2015]选数】
这道题自然是可以反演的
按照反演的套路我们先设出两个函数
之后就是如何表示
非常显然就是
了
把
但是这道题的
不能线筛杜教筛总可以了吧,于是就可以用
#include<iostream>
#include<cstring>
#include<cstdio>
#include<tr1/unordered_map>
#define re register
#define maxn 5000001
#define LL long long
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
const int mod=1000000007;
using namespace std::tr1;
unordered_map<int,int> ma;
int p[maxn>>1],f[maxn],mu[maxn];
inline LL quick(int a,int b)
{
LL S=1;
while(b) {if(b&1) S=S*a%mod;b>>=1;a=(LL)a*(LL)a%mod;}
return S;
}
int N,K,L,H,M;
int solve(int x)
{
if(x<=M) return mu[x];
if(ma.find(x)!=ma.end()) return ma[x];
int ans=1;
for(re int l=2,r;l<=x;l=r+1)
{
r=x/(x/l);
ans-=solve(x/l)*(r-l+1);
}
return ma[x]=ans;
}
int main()
{
scanf("%d%d%d%d",&N,&K,&L,&H);
H/=K;
if(L%K==0) L=L/K;
else L=L/K+1;
M=min(H,5000000);
mu[1]=f[1]=1;
for(re int i=2;i<=M;i++)
{
if(!f[i]) p[++p[0]]=i,mu[i]=-1;
for(re int j=1;j<=p[0]&&p[j]*i<=M;j++)
{
f[p[j]*i]=1;
if(i%p[j]==0) break;
mu[i*p[j]]=-1*mu[i];
}
}
for(re int i=1;i<=M;i++) mu[i]+=mu[i-1];
LL ans=0;L--;
for(re int l=1,r;l<=H;l=r+1)
{
if(!(L/l)) r=H/(H/l);
else r=min(H/(H/l),L/(L/l));
ans=(ans+quick(H/l-L/l,N)*(LL)(solve(r)-solve(l-1))%mod)%mod;
}
printf("%lld\n",((ans%mod)+mod)%mod);
return 0;
}