首页 > 代码库 > HDU 3709 Balanced Number 枚举+数位DP
HDU 3709 Balanced Number 枚举+数位DP
枚举支点之后数位DP,注意姿势
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;const int maxn = 20;int lim[maxn],len;int num[maxn];LL f[20][3000][20]; LL dfs(int now,int power,int piv,int bound) { if(now == 0) { return power == 0; } if(power < 0) return 0; LL ¬e = f[now][power][piv]; if(!bound && note != -1) return note; int m = bound ? lim[now - 1] : 9; LL ret = 0; for(int i = 0;i <= m;i++) { num[now - 1] = i; ret += dfs(now - 1,power + (now - 1 - piv) * i,piv,bound && i == m); } if(!bound) note = ret; return ret;}inline LL work(LL num) { len = 0; while(num) { lim[len++] = num % 10; num /= 10; } //枚举支点 LL ret = 0; memset(f,-1,sizeof(f)); for(int i = 0;i < len;i++) { ret += dfs(len,0,i,1); } return ret - len + 1;}int main() { int T; cin >> T; while(T--) { LL n,m; cin >> n >> m; cout << work(m) - work(n - 1) << endl; } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。