hdu-3555 Bomb(数位dp)

题目链接:

Bomb

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 131072/65536 K (Java/Others)


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
 
 
题意
 
问[1,n]中有多少含49的数;
 
思路
 
位数dp;
 
AC代码
 
#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e6+5e5;
LL dp[25][3],n;
int b[25];
int fun()
{
    mst(dp,0);
    dp[0][2]=1LL;
    for(int i=1;i<23;i++)
    {
        dp[i][0]=dp[i-1][0]*10+dp[i-1][1];
        dp[i][1]=dp[i-1][2];
        dp[i][2]=dp[i-1][2]*10-dp[i-1][1];
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    fun();
    while(t--)
    {
        scanf("%lld",&n);
        LL temp=n,ans=0;
        int cnt=1;
        while(temp)
        {
            b[cnt++]=temp%10;
            temp/=10;
        }
        b[cnt]=0;
        int flag=0;
        for(int i=cnt;i>0;i--)
        {
            ans+=dp[i-1][0]*(LL)b[i];
            if(b[i]>4&&!flag)
            {
                ans+=dp[i-1][1];
            }
            if(flag)
            {
                ans+=dp[i-1][2]*b[i];
            }
            if(b[i+1]==4&&b[i]==9)
            {
                flag=1;
            }
        }
        if(flag)ans++;
        printf("%lld
",ans);
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/zhangchengc919/p/5469197.html