PAT 1049 Counting Ones 数位dp
2014-08-25 11:31
288 查看
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<vector> #include<cmath> using namespace std; int bit[15],dp[15][15][2]; int nm,n; int solve(int nn,int up,int bd) { if(dp[nn][up][bd]!=-1)return dp[nn][up][bd]; if(nn == 1) { if(up>=1)return dp[nn][up][bd] = 1; else return dp[nn][up][bd] = 0; } if(!bd)return dp[nn][up][bd] = (up+1)*solve(nn-1,up,bd)+(int)pow(10,nn-1); else { dp[nn][up][bd] = up*solve(nn-1,9,0)+solve(nn-1,bit[nn-1],1); if(up == 1)dp[nn][up][bd]+=n%(int)pow(10,nn-1)+1; else if(up)dp[nn][up][bd]+=(int)pow(10,nn-1); return dp[nn][up][bd]; } } int main() { int tn; while(scanf("%d",&n)!=EOF) { nm = 1; tn = n; memset(dp,-1,sizeof(dp)); while(tn) { bit[nm++] = tn%10; tn/=10; } nm--; printf("%d\n",solve(nm,bit[nm],1)); } return 0; }
相关文章推荐
- PAT 1049. Counting Ones (30)(数位dp(记忆化搜索))
- 【PAT】1049. Counting Ones (30)
- PAT 1049 Counting Ones
- PAT 解题报告 1049. Counting Ones (30)
- PAT 1049. Counting Ones (30)
- 【PAT甲级】1049. Counting Ones (30)
- ZJU PAT 1049 Counting Ones
- PAT (Advanced Level) 1049. Counting Ones (30)
- PAT甲级 1049. Counting Ones (30)
- 【PAT 1049】 Counting Ones 数学规律
- PAT 1049. Counting Ones
- PAT甲级.1049. Counting Ones (30)
- 【PAT (Advanced Level)】1049. Counting Ones (30)
- 【PAT】【Advanced Level】1049. Counting Ones (30)
- PAT 甲级 1049. Counting Ones (30)
- pat 1049. Counting Ones (30)
- pat 1049. Counting Ones (30)
- PAT (Advanced Level) Practise 1049 Counting Ones (30)
- PAT 1049. Counting Ones (30)
- PAT1049:Counting Ones