第七场
F. Sumire
思路
- 记录状态 \(dp_{rem,pre}\) 剩余 \(rem\) 位,前面有 \(pre\) 个数位 d。
- 转移的时候不能枚举 B 个数,发现只有数位 = d 的时候对 pre 有更新,所以直接讨论就好了。
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> PII;
typedef std::pair<ll, ll> PLL;
typedef double db;
#define re _read
#define ALL(x) (x).begin(),(x).end()
#define SZ(v) ((int)v.size())
#define fi first
#define se second
#define pb push_back
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl "\n"
using namespace std;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x;}
template<class T>
inline void _read(T& x) {
static T ans;
static unsigned int c;
static bool p;
for (c = getchar(); c != '-' && (c < '0' || c > '9'); c = getchar());
if (c == '-') p = false, c = getchar(); else p = true;
for (ans = 0; c <= '9' && c >= '0'; c = getchar()) ans = ans * 10 + c - '0';
x = p ? ans : -ans;
}
/*----------------------------------------------------------------------------------------------------*/
const int mod = 1e9 + 7;
ll qmi(ll a, ll k, int mod) {
ll res = 1;
if (a == 0 && k == 0) return 0;
while (k) {
if (k & 1)
res = res * a % mod;
a = a * a % mod;
k >>= 1;
}
return res;
}
ll k, B, D, l, r;
ll dp[70][70];
ll dfs(int rem, int pre) {
ll& ans = dp[rem][pre];
if (ans != -1) return ans;
ans = 0;
if (!rem) return ans = qmi(pre, k, mod);
ans = dfs(rem - 1, pre + 1) + (B - 1) * dfs(rem - 1, pre) % mod;
if (ans >= mod) ans -= mod;
return ans;
}
ll solve(ll x) {
x ++;
vector<int> d;
while (x) {
d.pb(x % B);
x /= B;
}
reverse(ALL(d));
int m = d.size();
ll ans = 0;
for (int i = 1; i < m; i++) {
ans += (B - 2) * dfs(i - 1, 0) % mod;
if (ans >= mod) ans -= mod;
ans += dfs(i - 1, D != 0);
if (ans >= mod) ans -= mod;
}
int pre = 0;
for (int i = 0; i < m; i++) {
if (i == 0) {
if (!D) {
ans += (d[i] - 1) * dfs(m - i - 1, pre) % mod;
}
else {
if (d[i] > D) ans += (dfs(m - i - 1, pre + 1) + (d[i] - 2) * dfs(m - i - 1, pre) % mod) % mod;
else ans += (d[i] - 1) * dfs(m - i - 1, pre) % mod;
}
if (ans >= mod) ans -= mod;
}
else {
if (d[i] > D) ans += (dfs(m - i - 1, pre + 1) + (d[i] - 1) * dfs(m - i - 1, pre) % mod) % mod;
else ans += d[i] * dfs(m - i - 1, pre) % mod;
if (ans >= mod) ans -= mod;
}
if (d[i] == D) pre++;
}
return ans;
}
int main() {
int T;
re(T);
while (T--) {
memset(dp, -1, sizeof dp);
re(k), re(B), re(D), re(l), re(r);
printf("%lld\n", (solve(r) - solve(l - 1) + mod) % mod);
}
return 0;
}
原文地址:http://www.cnblogs.com/Roshin/p/HDU2022.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性