http://www.lightoj.com/volume_showproblem.php?problem=1140
给出区间[m,n],求区间内的所有数共有多少个0。
设dp[i][j]表示处理到第i位时,它前面共有j个0(除了前导零)。
调试了半天,少些了一句 != -1。。
#include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack> #include <vector> #include <math.h> #include <string.h> #include <queue> #include <string> #include <stdlib.h> #include <algorithm> //#define LL __int64 #define LL long long #define eps 1e-12 #define PI acos(-1.0) using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 4010; LL dp[25][25]; int dig[25]; LL dfs(int len, int first, int sta, int up) { if(len == 0) { if(first) return (LL)1; else return (LL)sta; } if(!up && dp[len][sta] != -1 && !first) return dp[len][sta]; int n = up ? dig[len] : 9; LL res = 0; for(int i = 0; i <= n; i++) { if(first) { res += dfs(len-1,first&&i==0,0,up&&i==n); } else { if(i == 0) res += dfs(len-1,0,sta+1,up&&i==n); else res += dfs(len-1,0,sta,up&&i==n); } } if(!up && !first) dp[len][sta] = res; return res; } LL cal(LL num) { int len = 0; if(num == 0) dig[++len] = 0; while(num) { dig[++len] = num % 10; num /= 10; } return dfs(len,1,0,1); } int main() { int test; LL a,b; scanf("%d",&test); for(int item = 1; item <= test; item++) { scanf("%lld %lld",&a,&b); memset(dp,-1,sizeof(dp)); printf("Case %d: %lld\n",item,cal(b) - cal(a-1) ); } return 0; }