HDU 3555 Bomb (数位dp)
Problem Description
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?
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
Input
The 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.
The input terminates by end of file marker.
Output
For each test case, output an integer indicating the final points of the power.
Sample Input
3
1
50
500
Sample Output
0 1 15
View Code
Hint
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.题意:
小于所给数字的数字有多少个包含49.
思路:
数位dp基础,详见代码
#include<iostream> #include<algorithm> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #define fuck(x) cout<<#x<<" = "<<x<<endl; #define ls (t<<1) #define rs ((t<<1)+1) using namespace std; typedef long long ll; typedef unsigned long long ull; const int maxn = 100086; const int inf = 2.1e9; const ll Inf = 999999999999999999; const int mod = 1000000007; const double eps = 1e-6; const double pi = acos(-1); int bit[20]; ll dp[20][4]; //sta表示三种状态。 //1:pos结尾处是4 //2:pos之前有49 //0:不含以上两种情况 ll dfs(int pos,int sta,bool limit){ if(pos==-1&&sta==2){return 1ll;} else if(pos==-1){return 0;} else if(!limit&&dp[pos][sta]!=-1){ return dp[pos][sta]; } int up=limit?bit[pos]:9; ll ans=0; for(int i=0;i<=up;i++){ if(sta==2||(sta==1&&i==9)){//之前有49或者刚刚凑齐一个 ans+=dfs(pos-1,2,limit&&i==up); } else if(i==4){//pos结尾处是4 ans+=dfs(pos-1,1,limit&&i==up); } else{ ans+=dfs(pos-1,0,limit&&i==up); } } if(!limit){dp[pos][sta]=ans;}//没有限制才能赋值给dp。 return ans; } ll solve(ll t){ int pos=0; while(t){ bit[pos++]=t%10; t/=10; } return dfs(pos-1,0,true); } int main() { int T; scanf("%d",&T); memset(dp,-1,sizeof(dp)); while(T--){ ll n; scanf("%lld",&n); printf("%lld\n",solve(n)); } return 0; }
如需转载,请注明出处
如有侵权,联系删除
2290713181@qq.com
如有侵权,联系删除
2290713181@qq.com