# UVALive - 7344 Numbered Cards [数位dp+状压dp]【动态规划】

——————————————————————————————————————————
You have N cards and each has an unique number between 1 and N written on it. In how many
ways can you select a non-empty subset of the cards such that the number written on any two of your selected cards don’t have any common digits?
For example, when N = 12, {1, 2, 3}, {2, 11}, {3, 4, 5, 6, 7, 8, 9, 12} are some valid selections. But
{1, 2, 10}, {2, 5, 12} are not allowed.

Input
The first line of the input contains an integer T (T15) $(T ≤ 15)$ which is the number of test cases. Each of
the following T lines denote a test case, containing an integer N (1N<109) $(1 ≤ N < 10^9)$.

Output
For each test case, output the case number followed by the number of subsets modulo 1000000007.

Sample Input
2
3
12

Sample Output
Case 1: 7
Case 2: 1151
————————————————————————————————————————————

————————————————————————————————————————————

LL dp_nex[15][(1<<12)];
int n;
int num[12],len;
int dp[12][1111];
int f[1111];

int dfs(int pos,int limit,int na,int &nb){
if(pos<0) return na==nb;
int &t=dp[pos][na];
if(!limit&&t!=-1) return t;

int endi = 9;if(limit) endi=num[pos];

int res = 0;
for(int i=0;i<=endi;i++){
if( (na|i) == 0 )
res+=dfs(pos-1,endi==i&&limit,na,nb);
else if(nb&(1<<i))
res+=dfs(pos-1,endi==i&&limit,na|(1<<i),nb);
}

if(!limit) t=res;
return res;
}

void solve(int x){

for(len=0;x;x/=10) num[len++]=x%10;

f[0]=f[1]=0;
for(int i=2;i<(1<<10);i++){
memset(dp,-1,sizeof(dp));
f[i]=dfs(len-1,1,0,i);
}
}

LL qmod(LL a,LL b){
LL res = 1;
while(b){
if(b&1) res=res*a%MOD;
b>>=1;  a  =a  *a%MOD;
}
return res;
}

int main(){
int t,kcase=0;scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);

memset(dp_nex,0,sizeof(dp_nex));

solve(n);

dp_nex[0][0]=1;
int endi=(1<<10);

for(int i=1;i<=10;i++){
for(int j=0;j<endi;j++){
for(int k=0;k<endi;k++){
if(j==k)continue;
if(((j&k)==k))
dp_nex[i][j]=(dp_nex[i][j]+dp_nex[i-1][k]*f[j-k])%MOD;
}
}
}

LL output=0;
for(int i=1;i<=10;i++){
int tmp=1;
for(int j=1;j<=i;j++) tmp*=j;

for(int j=0;j<endi;j++)
output=(output+qmod(tmp,MOD-2)*dp_nex[i][j])%MOD;
}
printf("Case %d: %lld\n",++kcase,output);
}
return 0;
}
08-22 301

12-12 424
07-24 212
08-05 179
08-08 24