标签:balance red eal hdu color ace res auth include
///@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; }
标签:balance red eal hdu color ace res auth include
原文地址:http://www.cnblogs.com/zjnu/p/7305768.html