【HDU 3555】 Bomb

【题目链接】

          点击打开链接

【算法】

         数位DP

【代码】

           

#include<bits/stdc++.h>
using namespace std;
#define MAXL 15
typedef unsigned long long ULL;

ULL T,n,a[MAXL];
ULL dp[MAXL][10];

template <typename T> inline void read(T &x) {
    ULL f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1)  + c -'0';
    x *= f;
}
template <typename T> inline void write(T x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x/10);
    putchar(x%10+'0');    
}
template <typename T> inline void writeln(T x) {
    write(x);
    puts("");    
}
inline void getdp() {
    ULL i,j,k;
    dp[0][0] = 1;
    for (i = 1; i <= MAXL; i++) {
        for (j = 0; j <= 9; j++) {
            for (k = 0; k <= 9; k++) {
                if (j == 4 && k == 9) continue;
                dp[i][j] += dp[i-1][k];                
            }
        }
    }    
}
inline ULL calc(ULL n) {
    ULL i,j,ans = 0;
    a[0] = 0;
    while (n != 0) {
        a[++a[0]] = n % 10;
        n /= 10;
    }    
    a[a[0]+1] = 0;
    for (i = a[0]; i >= 1; i--) {
        for (j = 0; j < a[i]; j++) {
            if (a[i+1] == 4 && j == 9) continue;
            ans += dp[i][j];
        }
        if (a[i+1] == 4 && a[i] == 9) break;
    }
    return ans;
} 

int main() {
    
    getdp();
    
    read(T);
    while (T--) {
        read(n);
      	writeln(n-calc(n+1)+1);
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/even_bao/article/details/80084244
今日推荐