1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include<bits/stdc++.h> #define Inf 0x3f3f3f3f #pragma GCC optimize(2) using namespace std; typedef long long LL; typedef pair<int,int> P; const int MAXX=100005; const LL mod=1000000007LL;
LL num[100],dp[70][70]; LL k,b,d,l,r;
LL fastpow(LL jj,LL kk){ if(jj==0LL) return 0LL; LL ret=1; while(kk){ if(kk&1) ret=ret*jj%mod; jj=jj*jj%mod; kk>>=1; } return ret; }
LL dfs(LL now,bool limit,bool nozero,LL sum){ if(!now) return fastpow(sum,k); if(!limit&&nozero&&dp[now][sum]!=-1) return dp[now][sum]; LL ret=0,up=limit?num[now]:b-1; if(d<=up){ ret+=dfs(now-1,(limit&&(d==up)),nozero||d,sum+(bool)(nozero||d)),ret%=mod; if(up==0); else{ if(d==0){ ret+=dfs(now-1,(limit),1,sum),ret%=mod; ret+=(up-1)*dfs(now-1,0,1,sum),ret%=mod; } else if(d==up){ ret+=dfs(now-1,0,nozero,sum),ret%=mod; ret+=(up-1)*dfs(now-1,0,1,sum),ret%=mod; } else{ ret+=dfs(now-1,0,nozero,sum),ret%=mod; ret+=dfs(now-1,(limit),1,sum),ret%=mod; ret+=(up-2)*dfs(now-1,0,1,sum),ret%=mod; } } } else{ if(up==0) ret+=dfs(now-1,(limit),nozero,sum),ret%=mod; else{ ret+=dfs(now-1,0,nozero,sum),ret%=mod; ret+=dfs(now-1,(limit),1,sum),ret%=mod; ret+=(up-1)*dfs(now-1,0,1,sum),ret%=mod; } }
if(!limit&&nozero) dp[now][sum]=ret; return ret; }
inline LL getans(LL x){ memset(dp,-1,sizeof(dp)); LL p=0; while(x){ num[++p]=x%b; x/=b; } return dfs(p,true,false,0LL); }
inline void solve(){ scanf("%lld%lld%lld%lld%lld",&k,&b,&d,&l,&r); printf("%lld\n",(getans(r)-getans(l-1)+mod)%mod); }
signed main(){ LL t;scanf("%lld",&t); while(t--) solve(); return 0; }
|