1. 程式人生 > >HDU 3555 Bomb (數位dp)

HDU 3555 Bomb (數位dp)

point test case problem first 代碼 show ane int amp

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? 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. 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 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; }
View Code

HDU 3555 Bomb (數位dp)