Description
to calculate the number of balanced numbers in a given range [x, y].
Input
Output
Sample Input
Sample Output
#include <cstdio> #include <iostream> #include <cstring> #include <string> using namespace std; typedef long long LL; int a[20], sum[20]; LL dp[20][20][1710]; int len; LL dfs(int u, int pivot, int torque, bool limit) { if(u < 1) return torque == 0; if(!limit && dp[u][pivot][torque] != -1) return dp[u][pivot][torque]; int maxn = limit ? a[u] : 9; LL ret = 0; int newT; for(int i=0; i<=maxn; i++) { newT = torque+(u-pivot)*i; if(newT >= 0) ret += dfs(u-1, pivot, newT, limit&&i==maxn); } if(!limit) dp[u][pivot][torque] = ret; return ret; } LL f(LL x) { len=0; while(x) { a[++len] = x%10; x /= 10; } LL ret = 0; for(int i=1; i<=len; i++) { ret += dfs(len, i, 0, 1); } return ret-len+1; } int main () { int T; LL x, y; scanf("%d", &T); memset(dp, -1, sizeof(dp)); while(T--) { scanf("%lld%lld", &x, &y); printf("%lld\n", f(y) - f(x-1)); } return 0; }
287



被折叠的 条评论
为什么被折叠?



