考虑H-L很小于是可以暴力枚举H-L中k的倍数
当然,莫比乌斯反演后使用map离散化进行杜教筛也可以过,甚至还可以无视H-L的条件
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
#define ll long long
ll n,k,l,r;
ll flag=0;
const ll mod=1e9+7;
ll quick_pow(ll x,ll k){
ll ret=1;
while(k){
if(k%2==1){
ret=(ret*x)%mod;
}
x=(x*x)%mod;
k/=2;
}
return ret;
}
ll f[400000]={0};
int main(){
// freopen("number.in","r",stdin);
// freopen("number.out","w",stdout);
cin>>n>>k>>l>>r;
if(l<=k&&k<=r){
flag=1;
}
l=(l-1)/k;
r=r/k;
ll Mx=r-l;
for(int i=Mx;i>=1;i--){
ll now=i;
ll L=l/now;
ll R=r/now;
if(L<R){
f[i]=((quick_pow((R-L),n)-(R-L))+mod)%mod;// delete choose all of itself because the GCD is itself
for(int j=(i<<1);j<=Mx;j+=i){
f[i]=((f[i]-f[j])+mod)%mod;
}
}
}
// for(int i=1;i<=Mx;i++){
// cout<<f[i]<<" ";
// }
cout<<f[1]+flag;
return 0;
}