HDU 3709: Balanced Number
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3709: Balanced Number相关的知识,希望对你有一定的参考价值。
Balanced Number
///@author Sycamore ///@date 8/8/2017 ///@ref kuangbin #include <bits/stdc++.h> typedef long long L; using namespace std; L dp[20][20][2000]; int place[20]; L DFS(int pos, int pivot, int sum, bool limited) { //pos=====================current digit //pivot===================current pivot //sum======temp sum between pos & pivot //limited===whether max(place[digit])==9 //dp=======solution to the current state if (pos == -1)return sum == 0 ? 1 : 0;//all digits visited if (sum<0)return 0;//prunning if (!limited&&~dp[pos][pivot][sum])//already figured out return dp[pos][pivot][sum]; int end = limited ? place[pos] : 9; L res = 0; for (int i = 0; i <= end; i++) res += DFS(pos - 1, pivot, sum + i*(pos - pivot), limited&&i == end); if (!limited)dp[pos][pivot][sum] = res; return res; } L calc(L n) { int len = 0; while (n) { place[len++] = n % 10; n /= 10; } L res = 0; for (int i = 0; i<len; i++) res += DFS(len - 1, i, 0, 1);//enumeration by pivots return res - (len - 1);//deals with duplicate 0‘s(00,000,...) } int main() { ios::sync_with_stdio(false); int T; L x, y; cin >> T; while (T--) { fill(**dp, **(dp+20), -1); cin >> x >> y; cout<<calc(y) - calc(x - 1)<<endl; } return 0; }
以上是关于HDU 3709: Balanced Number的主要内容,如果未能解决你的问题,请参考以下文章