Classy Numbers Codeforces Round 50 (数位DP)

output

standard output

Let's call some positive integer classy if its decimal representation contains no more than 33 non-zero digits. For example, numbers 44, 200000200000, 1020310203 are classy and numbers 42314231, 102306102306, 72774200007277420000 are not.

You are given a segment [L;R][L;R]. Count the number of classy integers xx such that L≤x≤RL≤x≤R.

Each testcase contains several segments, for each of them you are required to solve the problem separately.

Input

The first line contains a single integer TT (1≤T≤1041≤T≤104) — the number of segments in a testcase.

Each of the next TT lines contains two integers LiLi and RiRi (1≤Li≤Ri≤10181≤Li≤Ri≤1018).

Output

Print TT lines — the ii-th line should contain the number of classy integers on a segment [Li;Ri][Li;Ri].

Example

input

4
1 1000
1024 1024
65536 65536
999999 1000001

output

1000
1
0
2

数位DP 直接上板子

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll a[20];// 每一位的上限
ll dp[20][29]; //记忆化
ll dfs(ll pos,ll prev,ll limit)
{//     当前第几位  数位条件   是否受上限
    ll i;
    if(pos==0)
    {
        if(prev >= 4) return 0;
        else return 1;
    }
    if(!limit&&dp[pos][prev] != -1) return dp[pos][prev];
    ll up;
    ll ans = 0;
    up = limit?a[pos]:9;
    for(i=0;i<=up;i++)
    {
        if(i==0) ans += dfs(pos-1,prev,limit && i==up);
        else ans += dfs(pos-1,prev+1,limit && i==up);
    }
    if(!limit) dp[pos][prev] = ans;
    return ans;
}

ll solve(ll x)
{
    ll p = 0;
    while(x)
    {
        a[++p] = x%10;
        x /= 10;
    }
    return dfs(p,0,1);
}

int main()
{
    ll t,a,b;
    scanf("%lld",&t);
    memset(dp,-1,sizeof(dp));
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
       // cout<<solve(a-1)<<endl;
       // cout<<solve(b)<<endl;
        printf("%lld\n",solve(b) - solve(a-1));
    }
    return 0;

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值