HDU 3555 數位DP
阿新 • • 發佈:2018-01-31
rmi tail min container printf return 上界 swe .net The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
InputThe first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.
The input terminates by end of file marker.
OutputFor each test case, output an integer indicating the final points of the power.Sample Input
Sample Output
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
InputThe first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.
The input terminates by end of file marker.
OutputFor each test case, output an integer indicating the final points of the power.Sample Input
3 1 50 500
0 1 15Hint
From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.
題意:
問小於等於n的數字中包含著49的數字有多少個。
做法:
數位dp板子題目,統計有多少個不包含49的,減去就好
代碼:
#include<iostream> using namespace std; #include<cstdio> #include<cstring> #include<cstdlib> typedef long long ll; const int state = 2; int a[25]; ll dp[25][state]; ll dfs(int pos,int pre,bool limit){ if(pos == -1) return 1;//深度 int up;//枚舉上界 if(!limit&&dp[pos][pre]!=-1)//記憶化 return dp[pos][pre]; up = limit?a[pos]:9; ll ans = 0; for(int i=0;i<=up;i++){ if(pre==1&&i==9) continue; ans+=dfs(pos-1,i==4,limit&&i==a[pos]); } if(!limit)//有選擇地記憶化 return dp[pos][pre]=ans; else return ans; } ll solve(ll x){ int pos = 0; memset(a,0,sizeof(a)); while(x){ a[pos++]=x%10; x/=10; } return dfs(pos-1,0,true); } int main(){ ll p,q; memset(dp,-1,sizeof(dp)); int t; scanf("%d",&t); while(t--){ ll s; scanf("%lld",&s); p=solve(s); q = s - p + 1; printf("%lld\n",q); } return 0; }
這篇博客不錯,學習數位DP的童鞋可以看一下:
http://blog.csdn.net/wust_zzwh/article/details/52100392
HDU 3555 數位DP