Input
#include <cstdio> #include <iostream> #include <cstring> #include <string> using namespace std; typedef long long LL; int a[20]; LL dp[20][2][2]; LL dfs(int u, bool is4, bool have, bool limit) { if(u < 1) return have; if(!limit && dp[u][is4][have] != -1) return dp[u][is4][have]; int maxn = limit ? a[u] : 9; LL ret = 0; for(int i=0; i<=maxn; i++) { ret += dfs(u-1, i==4, have||(is4&&i==9), limit&&i==maxn); } if(!limit) dp[u][is4][have] = ret; return ret; } LL f(LL n) { int len=0; while(n) { a[++len] = n%10; n /= 10; } return dfs(len, 0, 0, 1); } int main () { int T; LL n; scanf("%d", &T); memset(dp, -1, sizeof(dp)); while(T--) { scanf("%lld", &n); printf("%lld\n", f(n)); } return 0; }
原文:http://www.cnblogs.com/AcIsFun/p/5389202.html