D - Bomb

 1 //反向62
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <string>
 5 #include <cstring>
 6 #include <cstdio>
 7 using namespace std;
 8 
 9 typedef long long ll;
10 // const int maxn = 1e5+5;
11 ll n;
12 ll a[100];
13 ll dp[100][100];
14 
15 //下标,前面是否4,是否有前导0, 是否有限制
16 ll dfs(ll pos, ll sta, ll pre, ll limit){
17     if(pos == -1) return 1;
18     if(!limit && dp[pos][sta] != -1)
19         return dp[pos][sta];
20     int up = limit?a[pos]:9;
21     ll ans = 0;
22     for(int i = 0;i <= up;i++){
23         if(pre == 4 && i == 9){
24             continue;
25         }
26         ans += dfs(pos-1, i == 4, i, limit && i == a[pos]);
27     }
28     if(!limit) dp[pos][sta] = ans;
29     return ans;
30 }
31 
32 ll solve(ll x){
33     ll pos = 0;
34     while(x){
35         a[pos++] = x%10;
36         x /= 10;
37     }
38     return dfs(pos-1, 0, -1, true);
39 }
40 
41 int main(){
42     memset(dp, -1, sizeof dp);
43     int t;
44     scanf("%d", &t);
45     while(t--){
46         scanf("%lld", &n);
47         ll ans = solve(n);
48         printf("%lld\n", n - ans + 1);
49     }
50     return 0;
51 }

猜你喜欢

转载自www.cnblogs.com/jaydenouyang/p/9118586.html