LightOJ - 1140 How Many Zeroes?
1140 - How Many Zeroes?
吉米写下m和n之间的所有自然数的小数表示,(mωn)。他将写多少个零?
思路:
数位DP
dp[pos][j]表示第pos位含有j个0的0的个数
#include<bits/stdc++.h> #define LL long long using namespace std; LL T,l,r,shu[20],dp[20][20]; LL dfs(LL pos,LL tot,bool lead,bool limit){ if(pos==0) return lead?1:tot;//当它枚举到最后一位还有前导零的话,那只能是0,处理0的情况 if(!lead&&!limit&&dp[pos][tot]) return dp[pos][tot]; LL cnt=0,up=limit?shu[pos]:9; for(LL i=0;i<=up;i++){ cnt+=dfs(pos-1,tot+(i==0&&!lead),lead&&i==0,limit&&i==shu[pos]); } if(!lead&&!limit) dp[pos][tot]=cnt; return cnt; } LL slove(LL x){ LL k=0; while(x){ shu[++k]=x%10; x/=10; } return dfs(k,0,true,true); } int main() { scanf("%lld",&T); for(LL i=1;i<=T;i++){ scanf("%lld%lld",&l,&r); memset(dp,0,sizeof(dp)); printf("Case %lld: %lld\n",i,slove(r)-slove(l-1)); } return 0; }
博主蒟蒻,若有出错的地方,敬请指出。
如有侵犯您版权的地方,请快速联系我,我会撤回本博文。