QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#49544 | #4543. Sumire | Yeruihua | AC ✓ | 272ms | 3776kb | C++ | 2.4kb | 2022-09-21 22:00:29 | 2022-09-21 22:00:30 |
Judging History
answer
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define N 100005
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
ll k, b, d, l, r;
ll dp[65][65][2][2]; //前i位,有j位是d,当前位是否有最高位限制,是否有前导0 的方案数
ll digit[65];
ll qpow(ll a,ll b){
if(a==0)
return 0;//0^0=0
ll ans = 1;
b %= mod;
while(b){
if(b&1)
ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
ll dfs(int pos,int cnt,int lim,int lead){
if(!pos)
return qpow(cnt, k);
if(dp[pos][cnt][lim][lead] != -1){
return dp[pos][cnt][lim][lead];
}
int up = lim ? digit[pos] : (b - 1);
ll res = 0;
if(d==up){
res += dfs(pos - 1, cnt + 1, lim, 0); //填d
if(lead){
res += dfs(pos - 1, cnt, 0, 1); //填0
res += (up - 1) * dfs(pos - 1, cnt, 0, 0) % mod; //不填up和0
}
else
res += up * dfs(pos - 1, cnt, 0, 0) % mod; //不填d
}
else if(d==0){
res += dfs(pos - 1, cnt, lim, 0);//填up
res += (up - 1) * dfs(pos - 1, cnt, 0, 0) % mod;//填up和d
if(lead){
res += dfs(pos - 1, cnt, 0, 1);//0
}
else
res += dfs(pos - 1, cnt + 1, 0, 0);//d
}
else if(up>d){
res += dfs(pos - 1, cnt + 1, 0, 0) + dfs(pos - 1, cnt, lim, 0);//d,up
if(lead){
res += dfs(pos - 1, cnt, 0, 1);//0
res += (up - 2) * dfs(pos - 1, cnt, 0, 0) % mod; // ! up,d,0
}
else
res += (up - 1) * dfs(pos - 1, cnt, 0, 0) % mod; // ! up,d
}
else{
res += dfs(pos - 1, cnt, lim, 0);// up
if(lead){
res += dfs(pos - 1, cnt, 0, 1);// 0
res += (up - 1) * dfs(pos - 1, cnt, 0, 0) % mod; //! 0,up
}
else
res += up * dfs(pos - 1, cnt, 0, 0) % mod; // !up
}
return dp[pos][cnt][lim][lead] = (res % mod);
}
ll cal(ll x){
memset(dp, -1, sizeof(dp));
int pos = 0;
while(x){
digit[++pos] = x % b;
x /= b;
}
return dfs(pos, 0, 1, 1);
}
int main(){
IO;
int T;
cin >> T;
while(T--){
cin >> k >> b >> d >> l >> r;
cout << (cal(r) - cal(l - 1) + mod) % mod << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 272ms
memory: 3776kb
input:
10000 3 207891369 69789899 964252340141144201 993463302889356041 3 747663427 196567324 607861784354966835 737019014793415965 7 4 1 75021281026163040 281275352760223247 159801714 6 4 57793681483331990 719907284728404544 760336565 3 2 640278753805042190 790056554571568287 3 3 0 370833904610234656 7289...
output:
348402080 920411258 659717287 394377489 330035010 995308641 312874977 375991223 465046227 263340736 704957449 599053093 345964631 546866401 355527840 906886371 165223493 924963116 97531004 174290409 257548623 852781447 29936714 204554940 926403980 440327688 351876140 824109444 816114821 745424255 69...
result:
ok 10000 lines