Jason的特殊爱好 [FZU 2113]
http://acm.fzu.edu.cn/problem.php?pid=2113
枚举1的个数,ans=ans+i*dfs();
View Code
const int MM = 11111; typedef __int64 int64; #define debug puts("wrong") int cnt, num[MM]; int64 dp[20][20][20],L,R; int64 dfs(int le,int sum,int s,bool less) { if(le==-1) return sum==s; if(!less && dp[le][sum][s]!=-1) return dp[le][sum][s]; int64 res=0; int d, e=less?num[le]:9; for(d=0;d<=e;d++) { if(d==1) res+=dfs(le-1,sum,s+1,less&&d==e); else res+=dfs(le-1,sum,s,less&&d==e); } if(!less) dp[le][sum][s]=res; return res; } int64 cal(int64 x) { for(cnt=0; x ;num[cnt++]=x%10,x/=10); int64 ans=0; for(int i=1;i<=cnt;i++) ans=ans+i*dfs(cnt-1,i,0,true); return ans; } void solve() { printf("%I64d\n",cal(R)-cal(L-1)); } int main() { memset(dp,-1,sizeof(dp)); while(scanf("%I64d%I64d",&L,&R)!=EOF) solve(); return 0; }