hdu3555(數位dp入門題)

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 6762    Accepted Submission(s): 2351


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.


數位dp模板題


#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<utility>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define Inf (1<<30)
#define LL unsigned long long
#define MOD 1000000009
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
LL dp[100][100];
void init()
{
    dp[0][0]=1;
    for(int i=1;i<=64;i++)//dp[i][j]表示長度是i的以j開頭的沒有49的字串個數
    {
        for(int j=0;j<=9;j++)
        {
            for(int k=0;k<=9;k++)
                if(!(k==9&&j==4))dp[i][j]+=dp[i-1][k];
        }
    }
}
LL query(LL x)
{
    LL a[70],L=0,ans=0;
    for(int i=1;x;i++)
    {
        a[i]=x%10;
        x/=10;
        L++;
    }
    a[L+1]=0;
    for(int i=L,j=0;i>=1;i--)
    {
        for(j=0;j<a[i];j++)
        {
            if(!(j==9&&a[i+1]==4))ans+=dp[i][j];
        }
        if(a[i]==9&&a[i+1]==4)break;
    }
    return ans;
}
int main()
{
    int T;
    LL n;
    init();
    //freopen("D:\\oo.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64u",&n);
        printf("%I64u\n",n+1-query(n+1));///查詢0到n,[0,n]的沒有49的個數
    }
    return 0;
}


發佈了123 篇原創文章 · 獲贊 14 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章