Bomb(数位DP)

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
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.

问1到n中有多少个数含有49。

把上面那句话再读一遍!

是多少个数含有49,不是这些数含有多少个49,我真是丢他一家大爷。

这题有两个思路,一个是像上面那题一样,你要找49,你可先找不是49的,然后减去。
第二种就是直接找。
直接找也有几个方法,一个是dfs记忆化搜索,把需要的状态带上;
一个是dp[i][3],表示不是的啊,不是的啊并且最高位怎么怎么样,满足条件的啊balalalal
还有一种就是我这种xjb乱写。

下面代码有毒,谨慎看。

dp[i][j][0]代表i长度j开头不满足的数
dp[i][j][1]代表i长度j开头满足的数

满足的数永远都是满足的,只有不满足向满足的转移,这是大局(菊)观。

然后就写出了不能再傻逼的转移

for(int i = 0;i < 10;i++)
        dp[1][i][0] = 1;
    for(int i = 2;i < 25;i++)
    {
        for(int j = 0;j < 10;j++)
        {
            if(j != 4)
            {
                for(int k = 0;k < 10;k++)
                {
                    dp[i][j][0] = dp[i][j][0] + dp[i - 1][k][0];
                }
            }
            else
            {
                for(int k = 0;k < 10;k++)
                {
                    if(k == 9)continue;
                    dp[i][j][0] = dp[i][j][0] + dp[i - 1][k][0];
                }
            }
            if(j != 4)
            {
                for(int k = 0;k < 10;k++)
                {
                    dp[i][j][1] = dp[i][j][1] + dp[i - 1][k][1];
                }
            }
            else
            {
                for(int k = 0;k < 10;k++)
                {
                    dp[i][j][1] = dp[i][j][1] + dp[i - 1][k][1];
                }
                dp[i][j][1] = dp[i][j][1] + dp[i - 1][9][0];
            }
        }
    }

接着就是和上面那题一样的写法,先处理出各位,然后从最高位向最低位进行枚举,但是和上一题不一样的是,49xxxx这种数是不好找的,必须每次枚举完后,判断bit中是否有连续的49,然后把剩下的字节加起来(如果后面没有数了,那么可以直接break滚蛋了),但是我们在调用函数的时候已经人为的+1了,数出来的是1到bit,
我们找的是0到bit - 1,区间长度是一样的,所以直接返回bit就好了。

剩下的看代码理解。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
ll dp[27][12][2],po[25];
void init()
{
    memset(dp,0,sizeof(dp));
    po[0] = 1;
    for(int i = 1;i < 25;i++)
        po[i] = po[i - 1]*10;
    for(int i = 0;i < 10;i++)
        dp[1][i][0] = 1;
    for(int i = 2;i < 25;i++)
    {
        for(int j = 0;j < 10;j++)
        {
            if(j != 4)
            {
                for(int k = 0;k < 10;k++)
                {
                    dp[i][j][0] = dp[i][j][0] + dp[i - 1][k][0];
                }
            }
            else
            {
                for(int k = 0;k < 10;k++)
                {
                    if(k == 9)continue;
                    dp[i][j][0] = dp[i][j][0] + dp[i - 1][k][0];
                }
            }
            if(j != 4)
            {
                for(int k = 0;k < 10;k++)
                {
                    dp[i][j][1] = dp[i][j][1] + dp[i - 1][k][1];
                }
            }
            else
            {
                for(int k = 0;k < 10;k++)
                {
                    dp[i][j][1] = dp[i][j][1] + dp[i - 1][k][1];
                }
                dp[i][j][1] = dp[i][j][1] + dp[i - 1][9][0];
            }
        }
    }
}
ll cal(ll x)
{
    int bit[70],len = 0;
    while(x)
    {
        bit[++len] = x%10;
        x = x/10;
    }
    bit[len + 1] = 0;
    ll ans = 0,ok = 0;
    for(int i = len;i;i--)
    {
        for(int j = 0;j < bit[i];j++)
        {
            ans = ans + dp[i][j][1];
        }
        if(bit[i + 1] == 4 && bit[i] == 9)
        {
            if(i == 1)break;
            ll temp = 0;
            i--;
            while(i)
            {
                temp = temp*10 + bit[i];
                i--;
            }
            ans = ans + temp;
            break;
        }
    }
    return ans;
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        scanf("%I64d",&n);
        printf("%I64d\n",cal(n + 1));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值