https://ac.nowcoder.com/acm/contest/338/F

C++版本一

题解:DFS

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <bitset>
using namespace std;
 
#define LL long long
int T;
LL n;
LL dp[20][4];
int a[20];
int sta[10] = {3, 0, 2, 0, 0, 1, 0, 0, 0, 0};
 
LL dfs(int pos, int statu, bool limit) {
    if(pos == -1) {
        return statu == 3? 1: 0;
    }
    if(!limit && dp[pos][statu] != -1) {
        return dp[pos][statu];
    }
    int up = limit? a[pos]: 9;
    LL ans = 0;
    for(int i = 0; i <= up; ++i) {
        if(statu == 3) {
            ans += dfs(pos - 1, 3, limit && i == a[pos]);
            continue;
        }
        if(i == 5) {
            ans += dfs(pos - 1, 1, limit && i == a[pos]);
            continue;
        }
        if(sta[i] == statu + 1) {
            ans += dfs(pos - 1, sta[i], limit && i == a[pos]);
        } else {
            ans += dfs(pos - 1, 0, limit && i == a[pos]);
        }
    }
    if(!limit) {
        dp[pos][statu] = ans;
    }
    return ans;
}
 
LL solve(LL x) {
    memset(dp, -1, sizeof(dp));
    int pos = 0;
    while(x != 0) {
        a[pos++] = x % 10;
        x /= 10;
    }
    return dfs(pos - 1, 0, true);
}
 
int main() {
    #ifdef Dmaxiya
    freopen("test.txt", "r", stdin);
    #endif // Dmaxiya
    ios::sync_with_stdio(false);
 
    scanf("%d", &T);
    while(T--) {
        scanf("%lld", &n);
        if(n < 520) {
            printf("0\n");
            continue;
        }
        printf("%lld\n", solve(n));
    }
 
    return 0;
}

C++版本二

题解:DFS

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
ll t,n,m,k,q,ans;
int a[20];
char str;
ll dp[20][100];
ll dfs(int pos,int pre,bool limit){
    if(pos==-1)return 1;
    if(dp[pos][pre]!=-1&&!limit)return dp[pos][pre];
    ll ret=0;
    int n=limit?a[pos]:9;
    for(int i=0;i<=n;i++){
        if(pre==52&&!i)
            continue;
        ret+=dfs(pos-1,(pre%10)*10+i,limit&&i==a[pos]);
    }
    if(!limit){
        dp[pos][pre]=ret;
    }
    //cout<<ret<<endl;
    return ret;

}
ll solve(ll x) {
    memset(dp, -1, sizeof(dp));
    int pos = 0;
    while(x != 0) {
        a[pos++] = x % 10;
        x /= 10;
    }
    return dfs(pos - 1, 0, true);
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    scanf("%lld",&t);
    while(t--){
        scanf("%lld",&n);
        if(n < 520) {
            printf("0\n");
            continue;
        }
        printf("%lld\n", n-solve(n)+1);
    }
    //cout << "Hello world!" << endl;
    return 0;
}