1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include<bits/stdc++.h> typedef long long ll; const int MAXN=11; int Dp[MAXN][MAXN][MAXN][2][2][2][2]; int Nums[MAXN]; ll dfs(int pos,int fir,int sec,bool cnt,bool four,bool eight,bool out) { if(four&&eight) return 0; if(!pos) return cnt; if(~Dp[pos][fir][sec][cnt][four][eight][out]) return Dp[pos][fir][sec][cnt][four][eight][out]; ll ret=0; int res=out?9:Nums[pos]; for(int i=0;i<=res;++i) ret+=dfs(pos-1,i,fir,cnt|(i==fir&&i==sec),four|(i==4),eight|(i==8),out|(i<res)); return Dp[pos][fir][sec][cnt][four][eight][out]=ret; } ll L,R,M; inline ll calc(ll x) { if(x<1e10) return 0; std::memset(Dp,-1,sizeof(Dp)); ll ret=0; int len; for(len=0;x;x/=10) Nums[++len]=x%10; for(int i=1;i<=Nums[len];++i) ret+=dfs(len-1,i,-1,0,(i==4),(i==8),(i<Nums[len])); return ret; } int main() { std::cin>>L>>R; std::cout<<calc(R)-calc(L-1); return 0; }
|