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<cstdio> #include<cstring> #include<string> using namespace std; typedef long long ll; const ll MOD=1e9+7; const int Maxn=20; int a[Maxn]; ll l,r,dp[Maxn][1500][20]; ll (int pos,int sum,int center,bool limit) { if(pos==0) return sum==0; if(sum<0) return 0; if(!limit && dp[pos][sum][center]!=-1) return dp[pos][sum][center]; int up=limit?a[pos]:9; ll res=0; for(int i=0;i<=up;i++) res+=dfs(pos-1,sum+i*(pos-center),center,limit&&i==up); if(!limit) dp[pos][sum][center]=res; return res; } ll solve(ll x) { int pos=0; while(x){ a[++pos]=x%10;x/=10; } ll res=0; for(int i=pos;i>=1;i--) res+=dfs(pos,0,i,1); return res-pos+1; } int main() { int T; scanf("%d",&T); memset(dp,-1,sizeof(dp)); while(T--) { scanf("%I64d%I64d",&l,&r); printf("%I64dn",solve(r)-solve(l-1)); } return 0; }
|
近期评论