思路:
经典数位dp
代码:
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) int cost[]={,,,,,,,,,,,,,,,};
int d[];
LL dp[][];
LL dfs(int pos,int sum,bool limit){
if(pos<)return sum;
if(!limit&&~dp[pos][sum])return dp[pos][sum];
int top=limit?d[pos]:;
LL t=;
for(int i=;i<=top;i++){
t+=dfs(pos-,sum+cost[i],limit&&i==d[pos]);
}
if(!limit)dp[pos][sum]=t;
return t;
}
LL solve(LL n){
for(int i=;i<;i++){
d[i]=n%;
n/=;
}
return dfs(,,true);
}
int main(){
LL n,m,l,r;
int T;
LL s=;
mem(dp,-);
scanf("%d",&T);
while(T--){
scanf("%lld%llX",&n,&m);
l=m;
r=m+n-;
if(r>s){
printf("%lld\n",solve(r-s-)+solve(s)-solve(l-));
}
else{
printf("%lld\n",solve(r)-solve(l-));
}
}
return ;
}