ll dfs(int*num, int n, int pre, int pos, bool limit, bool lead){//try to fill the pos bit if(pos==n)return1; else{ if(!limit&&dp[pre==6][num[pos]][n-pos][lead]!=-1) return dp[pre==6][num[pos]][n-pos][lead]; else { int upper=limit ? num[pos] : 9; ll ans=0; for(int i=0;i<=upper;i++){ if(i==4)continue; if(pre==6&&i==2)continue; ans+=dfs(num,n,i,pos+1,limit&&i==upper,lead||i!=0); } if(!limit) dp[pre==6][num[pos]][n-pos][lead]=ans; return ans; } } }