HDOJ3555 Bomb【数位DP】

Bomb

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


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 <bits/stdc++.h>
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
long long dp[20][3];
void init()
{
    mst(dp);
    dp[0][2]=1;
    f(i,1,20)
    {
        dp[i][0]=dp[i-1][0]*10+dp[i-1][1]; //含49
        dp[i][1]=dp[i-1][2]; //不含49但最高位为9
        dp[i][2]=dp[i-1][2]*10-dp[i-1][1]; //不含49且最高位不为9
    }
}
long long fun(long long n)
{
    int len=1;
    int bit[25];
    while(n)
    {
        bit[len++]=n%10;
        n/=10;
    }
    bit[len]=0;
    int flag=0;
    long long sum=0;
    for(int i=len;i>0;i--)
    {
        sum+=bit[i]*dp[i-1][0];
        if(flag)
            sum+=bit[i]*dp[i-1][2];
        if(!flag&&bit[i]>4)
            sum+=dp[i-1][1];
        if(bit[i+1]==4&&bit[i]==9)
            flag=1;
    }
    return sum;
}
int main()
{
    init();
    int t;
    long long n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d",&n);
        printf("%I64d\n",fun(n+1));
    }
    return 0;
}

法二(记忆化搜索)

#include <bits/stdc++.h>
#define mst(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int bit[20];
long long dp[20][3];
//0:没有49且最后一位不为4,1:没有49且最后一位为4,2:有49
//flag=1;无限制,flag=0,有限制
long long dfs(int pos,int st,int flag)
{
    if(pos==0) return st==2;
    if(flag&&dp[pos][st]!=-1) return dp[pos][st];
    long long sum=0;
    int x=flag?9:bit[pos];
    f(i,0,x)
    {
        if((st==2)||(st==1&&i==9))
            sum+=dfs(pos-1,2,flag||i<x);
        else if(i==4) sum+=dfs(pos-1,1,flag||i<x);
        else sum+=dfs(pos-1,0,flag||i<x);
    }
    if(flag) dp[pos][st]=sum;
    return sum;
}
long long cal(long long x)
{
    int len=0;
    while(x)
    {
        bit[++len]=x%10;
        x/=10;
    }
    return dfs(len,0,0);
}
int main()
{
    int t;
    scanf("%d",&t);
    mst(dp);
    while(t--)
    {
        long long n;
        scanf("%I64d",&n);
        printf("%I64d\n",cal(n));
    }
    return 0;
}



法三:直接DP

用dp[i][j]表示所有以j开始的i位数中不满足条件的个数。这个直接暴力预处理即可。

然后就利用dp数组算出1~n中不满足的个数,减一下即可。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 25;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-6;

ll fac[maxn];
ll dp[maxn][10];
int digit[10];

void init()
{
    for(int i=0;i<=9;i++) dp[1][i]=1;
    for(int i=2;i<=18;i++)
    for(int j=0;j<=9;j++)
    for(int k=0;k<=9;k++)
    {
        if(j!=4||k!=9) dp[i][j]+=dp[i-1][k];
    }
}

ll solve(ll x)
{
    int len=0;
    mst(digit,0);
    ll tmp=x;
    while(x)
    {
        digit[++len]=x%10;
        x/=10;
    }
    ll ans=0;
    for(int i=1;i<len;i++)
    for(int j=1;j<=9;j++)
    {
        ans+=dp[i][j];
    }
    for(int i=1;i<digit[len];i++) ans+=dp[len][i];
    if(len==1) ans++;          //特判
    ll pre=digit[len];
    for(int i=len-1;i>0;i--)
    {
        tmp=digit[i];
        if(i==1)
        {
            for(int j=0;j<=tmp;j++)
            {
                if(pre!=4||j!=9) ans+=dp[i][j];
            }
        }
        else
        {
            for(int j=0;j<tmp;j++)
            {
                if(pre!=4||j!=9) ans+=dp[i][j];
            }
        }
        if(pre==4&&tmp==9) break;
        pre=tmp;
    }
    return ans;
}

int main()
{
    init();
    rush()
    {
        ll n;
        scanf("%lld",&n);
        printf("%lld\n",n-solve(n));
    }
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值