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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include <bits/stdc++.h> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define ff(i,j,k) for(int i=(j),end_i=(k);i< end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) #define DEBUG(x) cerr<<#x<<"="<<x<<endl #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&-(x)) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define mp make_pair #define fi first #define se second inline ll read() { ll x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); return f?-x:x; } #define CASET fo(___,1,read()) int d; ll c[20][20],fac[20]; long db f[20],g[20]; inline void init() { fo(i,0,19) { c[i][0]=c[i][i]=1; fo(j,1,i-1) c[i][j]=c[i-1][j]+c[i-1][j-1]; } fac[0]=1; fo(i,1,18) fac[i]=fac[i-1]*i; } int st[20],top,s[20]; inline ll calc(int k,int v) { if(!k) return 1; fo(i,0,18) f[i]=0; f[0]=fac[k]; fd(j,9,0) if(j^d) { fo(x,0,k) fo(y,0,min(k-x,v-s[j]-1)) g[x+y]+=f[x]/fac[y]; fo(x,0,k) f[x]=g[x],g[x]=0; } return (ll)(f[k]); } inline ll work(int k) { ll ans=0; int mx=0; fo(i,0,9) if(i^d) mx=max(mx,s[i]); fo(j,0,k) if(s[d]+j>mx) ans+=c[k][j]*calc(k-j,s[d]+j); return ans; } inline ll check() { int mx=0; fo(i,0,9) if(i^d) mx=max(mx,s[i]); return s[d]>mx; } ll dfs(int k,bool limit,bool flag) { if(!limit&&flag) return work(top-k+1); if(k>top) return check(); int mx=limit?st[k]-1:9; bool nex; ll ans=0; fo(i,0,mx) { nex=flag|(i!=0); if(nex) s[i]++; ans+=dfs(k+1,0,nex); if(nex) s[i]--; } if(limit) { s[st[k]]++; ans+=dfs(k+1,1,1); s[st[k]]--; } return ans; }
inline ll solve(ll r) { if(!r) return 0; fo(i,0,9) st[i]=0; for(top=0;r;st[++top]=r%10,r/=10); reverse(st+1,st+top+1); return dfs(1,1,0); } ll l,r; int main() { init(); CASET { l=read(); r=read(); d=read(); printf("%lld\n",solve(r)-solve(l-1)); } return 0; }
|