题目传送门:https://www.luogu.org/problem/show?pid=3172
题目分析:我也不想说什么了,把一道简单的题想复杂了。一开始想错思路,以为提取出k和k平方的个数,两次组合数搞容斥。结果发现错的离谱(2k,3k等情况没有讨论)。然后想莫比乌斯反演(好像时间过不去呀),而且我也不知道怎么log(n)求组合数(因为不能预处理阶乘到n),还上网看了个log(n)求阶乘,结果发现……我看错题了!可以重复选,这样就是个快速幂了。然而时间复杂度还是过不去,最后上网看了题解,发现做法好神啊。(%%%:http://blog.csdn.net/your_own_name/article/details/52801442)
CODE:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=100100;
const int M=1000000007;
int num[maxn];
int n,k,l,h;
int Fast_power(int a,int x)
{
if (x==1) return a;
long long mid=Fast_power(a,x>>1);
mid=mid*mid%M;
if (x&1) mid=mid*a%M;
return (int)mid;
}
int main()
{
freopen("c.in","r",stdin);
freopen("c.out","w",stdout);
scanf("%d%d%d%d",&n,&k,&l,&h);
l=(l+k-1)/k;
h=h/k;
for (int i=h-l; i>=1; i--)
{
int x=(l+i-1)/i-1;
int y=h/i-x;
num[i]=Fast_power(y,n);
num[i]=(num[i]-y+M)%M;
for (int j=2; i*j<=h-l; j++) num[i]=(num[i]-num[i*j]+M)%M;
}
if ( l==1 && h>=1 ) num[1]=(num[1]+1)%M;
printf("%d\n",num[1]);
return 0;
}