# 解题报告：HDU_6169 Senior PanⅡ (记忆化搜索)

1<=L,R<=1e11 ， 2<=K<=1e11

思路：

#include<bits/stdc++.h>

#define LL long long
const int N = 32e4 , M = 1e3 , K = 1e3;
const LL mod = 1e9+7;
const LL ni2 = (mod+1)>>1;
using namespace std;

LL dp[M][K] ;
vector<int>pr;
bool Np[N];

void init(){
for(int i=2;i<N;i++){if(!Np[i])pr.emplace_back(i);
for(int j=0,k=pr[0]*i;k<N;k=pr[++j]*i){
Np[k]=1;if(i%pr[j]==0)break;
}
}
}

bool check(LL x){
for(int i=0,ed=sqrt(x+0.5);pr[i]<=ed;i++){
int& j = pr[i];
if(x%j==0)return x==j;
}return true;
}

inline LL dfs(LL n,int k){
if(k<0)return  n % mod * (1+n%mod)  % mod * ni2 % mod;
if(pr[k] >=n)return (n>0) ;
if(n<M&&k<K){
LL& x = dp[n][k];
if(x>=0)return x;
return x =  ( dfs(n,k-1) - 1LL * pr[k] * dfs(n/pr[k],k-1) % mod + mod ) % mod;
}return ( dfs(n,k-1) - 1LL * pr[k] * dfs(n/pr[k],k-1) ) % mod ;
}

int main()
{
//freopen("1009.in","r",stdin);
//freopen("my_1009.out","w",stdout);
init();
int T,Cas=0;
scanf("%d",&T);
memset(dp,-1,sizeof(dp));
while(T--){
LL l,r,k;
scanf("%lld%lld%lld",&l,&r,&k);
printf("Case #%d: ",++Cas);
if(!check(k))printf("0\n");
else if(r/k<k)printf("%lld\n",k%mod*(k>=l&&k<=r));
else {
int p = lower_bound(pr.begin(),pr.end(),k)-pr.begin();
LL ans = k % mod * ( dfs(r/k,p-1) - dfs((l-1)/k,p-1) )  ;
ans = (ans%mod+mod)%mod;
printf("%lld\n",ans);
}
}return 0;
}