hdu3555(数位dp入门题)

来源:互联网 发布:程序员 明基 不闪屏 编辑:程序博客网 时间:2024/05/01 21:13

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
3150500
 

Sample Output
0115
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;}


0 0
原创粉丝点击