不要49代码:
#include <set> #include <map> #include <cmath> #include <stack> #include <queue> #include <vector> #include <string> #include <cstdio> #include <cstring> #include <sstream> #include <iomanip> #include <iostream> #include <algorithm> #define clr(str,x) memset(str,x,sizeof(str)) #define FRER() freopen("in.txt","r",stdin); #define FREW() freopen("out.txt","w",stdout); #define MAX_INF 0x7fffffff #define INF 0x3f3f3f3f #define maxn 100 typedef long long int ll; using namespace std; int digit[maxn]; /// 记录每一位上的数字 ll dp[maxn][2]; /// 第i位上 是否为4,符合条件的个数 ll dfs(int len,bool is4,bool limit) ///len是第几位,is4是上一位是否为4,limit是否达到上界 { if(len == 0) return 1; ///如果只有各位数字,直接返回1 if(!limit && dp[len][is4] != -1) ///记忆化数组 return dp[len][is4]; int up_bound = limit ? digit[len]:9; ///求当前位的上界 ll cnt = 0; for(int i=0; i<=up_bound; i++) { if(is4 && i==9) continue; ///是49 直接跳过 cnt+=dfs(len-1,i==4,limit&&i==up_bound);///继续搜索下一位 } if(!limit) dp[len][is4] = cnt; return cnt; } ll solve(ll num) { clr(digit,0); int len=0; while(num>0) { digit[++len]=num%10; num/=10; } clr(dp,-1); return dfs(len,false,true); } int main() { //FRER() //FREW() int T; ll n; scanf("%d",&T); while(T--) { scanf("%lld",&n); printf("%lld\n",n-solve(n)+1); } return 0; }
不要62代码:
#include <set> #include <map> #include <cmath> #include <stack> #include <queue> #include <vector> #include <string> #include <cstdio> #include <cstring> #include <sstream> #include <iomanip> #include <iostream> #include <algorithm> #define clr(str,x) memset(str,x,sizeof(str)) #define FRER() freopen("in.txt","r",stdin); #define FREW() freopen("out.txt","w",stdout); #define MAX_INF 0x7fffffff #define INF 0x3f3f3f3f #define maxn 100 typedef long long int ll; using namespace std; int dp[maxn][2]; int digit[maxn]; ll dfs(int len,bool if6,bool limit) { if(len == 0) return 1; if(!limit && dp[len][if6]!=-1) return dp[len][if6]; int up_bound = limit ? digit[len] : 9; ll cnt = 0; for(int i=0; i<=up_bound; i++) { if(if6 && i==2) continue; if(i==4) continue; cnt+=dfs(len-1,i==6,limit && i==up_bound); } if(!limit) dp[len][if6] = cnt; return cnt; } ll solve(ll num) { clr(digit,0); int len=0; while(num>0) { digit[++len] = num%10; num/=10; } clr(dp,-1); return dfs(len,false,true); } int main() { //FRER() //FREW() ll n,m; while(scanf("%lld%lld",&n,&m) && (n+m)) { printf("%lld\n",solve(m)-solve(n-1)); } return 0; }