dp[dep][ex][sta]表示长度为dep的,前面出现过的数的集合为ex,不满足要求的数字的集合为sta的满足要求的数的个数
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll dp[20][1024][1024]; 5 int dig[20],tmp; 6 ll dfs(int dep,int ex,int sta,int zero,int flag){ 7 if(!dep)return (ex&sta)==0; 8 if(!flag&&dp[dep][ex][sta]!=-1)return dp[dep][ex][sta]; 9 int lim=flag?dig[dep]:9; 10 ll ans=0; 11 for(int i=0;i<=lim;i++){ 12 if(zero&&i==0)ans+=dfs(dep-1,0,tmp,1,flag&(i==lim)); 13 else ans+=dfs(dep-1,ex|(1<<i),sta^(1<<i),0,flag&(i==lim)); 14 } 15 if(!flag)dp[dep][ex][sta]=ans; 16 return ans; 17 } 18 ll solve(ll x){ 19 int dd=0; 20 while(x)dig[++dd]=x%10,x/=10; 21 return dfs(dd,0,tmp,1,1); 22 } 23 int main(){ 24 memset(dp,-1,sizeof(dp)); 25 for(int i=0;i<10;i+=2)tmp|=(1<<i); 26 int T; 27 scanf("%d",&T); 28 while(T--){ 29 ll A,B; 30 scanf("%lld%lld",&A,&B); 31 printf("%lld\n",solve(B)-solve(A-1)); 32 } 33 return 0; 34 }
时间: 2024-11-12 00:11:53