找含有49的个数

这是一个编程题目,目标是找出所有包含数字49的整数个数,但处理的是非常大的数值。通过深度优先搜索(DFS)算法解决,考虑到数字限制和回溯,避免重复计算。代码中定义了动态规划数组并使用了递归函数进行求解。
摘要由CSDN通过智能技术生成

https://cn.vjudge.net/contest/194559#problem/H

此题和62的类似。就是找含有49的个数,但是重点在于要求的数很大,一直没过。


#include<iostream>
#include<stdio.h>
#include<string.h>


#define LL __int64
using namespace std;


LL dp[60][5];
long long dig[60];
LL dfs(long long pos,long long pre,long long status,bool limit)
{
    if(pos==-1)
        return 1;
    if(!limit&&dp[pos][status]!=-1)
        return dp[pos][status];
    long long up=limit?dig[pos]:9;
    long long temp=0;
    for(long long i=0;i<=up;i++)
    {
        if((pre==4&&i==9))
            continue;
            temp+=dfs(pos-1,i,i==4,limit&&i==dig[pos]);
    }
    if(!limit)
        return dp[pos][status]=temp;
    else
        return temp;
}
LL solve(LL x)
{
    LL lala=x;
    LL hi=0;
    while(x)
    {
        dig[hi++]=x%10;
        x/=10;
    }
    return lala+1-dfs(hi-1,-1,0,true);
}
int main()
{
    long long l;
    int yy;
    memset(dp,-1,sizeof(dp));
    cin>>yy;
    for(int i=1;i<=yy;i++)
    {
        scanf("%I64d",&l);
        printf("%I64d\n",solve(l));
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值