首页 > 代码库 > HDU 3709: Balanced Number
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。