Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3988 Accepted Submission(s): 1869
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define ll long long int dig[20]; ll dp[20][20][2000]; ll dfs(int pos, int pivot, int sum, int lim) { if(pos == -1) return sum == 0; if(sum < 0) return 0; if(!lim && dp[pos][pivot][sum] != -1) return dp[pos][pivot][sum]; int End = lim ? dig[pos] : 9; ll ret = 0; for(int i = 0; i <= End; i++) { ret += dfs(pos - 1, pivot, sum + i * (pos - pivot), (i == End) && lim); } if(!lim) dp[pos][pivot][sum] = ret; return ret; } ll func(ll num) { int n = 0; while(num) { dig[n++] = num % 10; num /= 10; } ll ret = 0; for(int i = 0; i < n; i++) { ret += dfs(n - 1, i, 0, 1); } return ret - n; } int main() { int t; ll n, m; memset(dp, -1, sizeof(dp)); scanf("%d", &t); while(t--) { scanf("%I64d %I64d", &n, &m); printf("%I64d\n", func(m) - func(n - 1)); } }
原文:http://www.cnblogs.com/lonewanderer/p/5656642.html