HDU3555 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?

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

这道题有坑的地方在于数据量,位数的话2的63次方是9223372036854775808(19位),被调函数也要开long long,wa了好几发。其他的套模板就行了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
using namespace std;
typedef long long int LL;
const int MAXL(1e5);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
LL dp[30][20];
LL a[30];
void init()
{
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(int i=1; i<=20; i++)
    {
        for(int j=0; j<=9; j++)
        {
            if(j==4)
            {
                for(int k=0; k<=9; k++)
                {
                    if(k!=9)
                        dp[i][j]+=dp[i-1][k];
                }
            }
            else
            {
                for(int k=0; k<=9; k++)
                {
                    dp[i][j]+=dp[i-1][k];
                }
            }
        }
    }
}

LL solve(LL n)
{
    LL len=0;
    LL ans=0;
    memset(a,0,sizeof(a));
    while(n)
    {
        a[++len]=n%10;
        n/=10;
    }
    for(LL i=len; i>=1; i--)
    {
        for(LL j=0; j<a[i]; j++)
        {
            if(j==9&&a[i+1]==4)
                continue;
            else
                ans+=dp[i][j];
        }
        if(a[i+1]==4&&a[i]==9)
            break;
    }
    return ans;
}


int main()
{

    LL ans1,n,m;
    init();
    scanf("%lld",&n);
    while(n--)
    {
        scanf("%lld",&m);
        ans1=solve(m+1);
        printf("%lld\n",m-ans1+1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shezjoe/article/details/81532576