HDU 3555 Bomb 数位dp

类似这道题目:HDU 2089 不要62

讲解就略过了,和上面那题完全一样。

#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long int LL;
LL dp[65][10];

void init()
{
    for (int i = 0; i <= 9; i++)
        dp[1][i] = 1;
    for (int i = 2; i <= 63; 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 cal(LL n)
{
    int digit[65], len = 0;
    memset(digit, 0, sizeof(digit));
    while (n)
    {
        digit[++len] = n % 10;
        n /= 10;
    }
    LL ans = 0;
    for (int i = len; i >= 1; i--)
    {
        for (int j = 0; j < digit[i]; j++)
            ans += dp[i][j];
        if (digit[i] == 9 && digit[i + 1] == 4)
            break;
    }
    return ans;
}

int main()
{
	//freopen("test.txt", "r", stdin);

    int T;
    scanf("%d", &T);
    init();
    while (T--)
    {
        LL N;
        scanf("%I64d", &N);
        printf("%I64d\n", N - cal(N + 1) + 1);
    }
	return 0;
}


你可能感兴趣的:(ACM-数位dp)