HDU-3555:Bomb

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?

数位DP,恶心。莫名狂WA。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
int t;LL a[50];
LL n,f[50][5],ans;
int work(LL x)
{
    int re=0;
    while(x) a[++re]=x%10,x/=10;
    return re;
}
int main()
{
    scanf("%d",&t);
    f[0][0]=1;
    for(int i=1;i<=22;i++)
    {
        f[i][0]=f[i-1][0]*10-f[i-1][1];
        f[i][1]=f[i-1][0];
        f[i][2]=f[i-1][1]+f[i-1][2]*10;
    }
    while(t--)
    {
        ans=0;
        scanf("%lld",&n);n++;
        int len=work(n);
        a[len+1]=0;
        bool flag=0;
        for(int i=len;i>0;i--)
        {
            ans+=f[i-1][2]*a[i];
            if(flag) ans+=f[i-1][0]*a[i];
            if(!flag&&a[i]>4)ans+=f[i-1][1];
            if(a[i+1]==4&&a[i]==9) flag=1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注