hdu3555

基本的数位dp

#include <cstdio>
#include <cstring>
using namespace std;

#define D(x) x

const int MAX_DIGIT = 66;

long long n;
int f[MAX_DIGIT];
long long memoize[MAX_DIGIT][2][2][2];

void to_digits(long long a)
{
    for (int i = 0; i < MAX_DIGIT; i++)
    {
        f[i] = a % 10;
        a /= 10;
    }
}

long long dfs(int digit, bool less, bool contain, bool four)
{
    if (digit == -1)
    {
        return contain;
    }
    if (memoize[digit][less][contain][four] != -1)
    {
        return memoize[digit][less][contain][four];
    }
    int limit = less ? 9 : f[digit];
    long long ret = 0;
    for (int i = 0; i <= limit; i++)
    {
        if (four && i == 9)
        {
            ret += dfs(digit - 1, less || i < f[digit], true, false);
            continue;
        }
        if (i == 4)
        {
            ret += dfs(digit - 1, less || i < f[digit], contain, true);
            continue;
        }
        ret += dfs(digit - 1, less || i < f[digit], contain, false);
    }
    memoize[digit][less][contain][four] = ret;
    return ret;
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%I64d", &n);
        to_digits(n);
        memset(memoize, -1, sizeof(memoize));
        long long ans = dfs(64, false, false, false);
        printf("%I64d\n", ans);
    }
    return 0;
}

时间: 2024-10-12 18:08:37

hdu3555的相关文章

【数位dp】hdu3555 Bomb

题意就是找0到n有多少个数中含有49.数据范围接近10^20 DP的状态是2维的dp[len][3]dp[len][0] 代表长度为len不含49的方案数dp[len][1] 代表长度为len不含49但是以9开头的数字的方案数dp[len][2] 代表长度为len含有49的方案数 状态转移如下dp[i][0] = dp[i-1][0] * 10 - dp[i-1][1];  // not include 49  如果不含49且,在前面可以填上0-9 但是要减去dp[i-1][1] 因为4会和9构

hdu3555 Bomb (记忆化搜索 数位DP)

http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7316    Accepted Submission(s): 2551 Problem Description The counter-terrorists found a time

[HDU3555]Bomb

试题描述 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 o

hdu3555 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=3555 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 nu

hdu3652 &amp;&amp; hdu 4722 &amp;&amp; hdu3555

数位dp简单题 hdu3652 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; const int N = 10+10; int dig[N]; ll dp[N][N][2][20]; ll dfs(int len,int pre,int is,int sum,int f)

数位DP初步 bzoj1026 hdu2089 hdu3555

为了搞SCOI的几道题先做水数位.之前听过课,半懂不懂吧,现在清楚了些. 这类题一般满足区间减法,即只需要我们求出(1,n)即可,然后打表也是为了sovle(DataType)服务.先想好怎么计算,再去想怎么打表.计算是一般存在这样的问题,就是比如n=abcdef,当a=6时,6开头的不能全算,那就只能先算1~5,然后理解为把6摆好,算下一位,这样我们发现,这个函数是没有包含n这个数的,所以调用是要调用sovle(n+1) 不要62 Problem Description 杭州人称那些傻乎乎粘嗒

hdu---(3555)Bomb(数位dp(入门))

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 7921    Accepted Submission(s): 2778 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists

hdu3555 Bomb(数位dp)

Bomb                                                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)                                                                             

数位dp hdu3555

#include <cstdio> #include <cstring> #include <set> #include <iostream> #include <map> #include <math.h> #include <algorithm> using namespace std; typedef long long ll; const int N = 1000005; int t,cnt,num[25]; ll