UVa 11361 Investigating Div-Sum Property (数位dp)
2021/8/15 6:38:25
本文主要是介绍UVa 11361 Investigating Div-Sum Property (数位dp),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2346
唯一的问题就是 \(k\) 太大数组开不下,但发现所有位数之和最多不会超过 \(90\),也就是 \(k\) 大于 \(90\) 一定无解
#include<cstdio> #include<cstring> #include<vector> using namespace std; typedef long long ll; const int maxn = 105; int T, k; ll A, B; ll dp[maxn][maxn][maxn][2]; vector<int> dig; ll dfs(int pos, int sum, int sel, int lim){ if(pos == 0) { if(sum == 0 && sel == 0) return 1; else return 0; } if(dp[pos][sum][sel][lim] != -1) return dp[pos][sum][sel][lim]; int up = lim ? dig[pos] : 9; ll res = 0; for(int i = 0 ; i <= up ; ++i){ res += dfs(pos-1, (sum*10+i)%k, (sel+i)%k, lim & (i == up)); } dp[pos][sum][sel][lim] = res; return res; } ll solve(ll x){ dig.clear(); memset(dp, -1, sizeof(dp)); dig.push_back(-1); ll tmp = x; while(tmp){ dig.push_back(tmp%10); tmp /= 10; } return dfs(dig.size()-1, 0, 0, 1); } int main(){ int T; scanf("%d", &T); for(int kase = 1 ; kase <= T ; ++kase){ scanf("%lld%lld%d", &A, &B, &k); if(k > 90) printf("0\n"); else printf("%lld\n", solve(B) - solve(A-1)); } return 0; }
这篇关于UVa 11361 Investigating Div-Sum Property (数位dp)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)