Classy Numbers (数位dp)

https://cn.vjudge.net/contest/312853#problem/G

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

题意:如果一个数的十进制表示没有超过3个非零的数的话就称之为classy数,现在给你区间a,b求之间有多少个这样的数

 裸的数位dp,sta表示非零数的个数就ok了

#include<bits/stdc++.h>
//#include<cstdio>
//#include<algorithm>
//#include<cstring>
//#include<map>
//#include<iostream>
#pragma GCC optimize(3)
#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
ll dp[25][25];
int a[25];
ll dfs(int pos,int sta,bool limit){
	if(pos==0) return sta<=3;
	if(!limit&&dp[pos][sta]!=-1) return dp[pos][sta];
	int up=limit?a[pos]:9;
	ll ans=0;
    for(int i=0;i<=up;i++){
    	if(i) ans+=dfs(pos-1,sta+1,limit&&i==up);
		else ans+=dfs(pos-1,sta,limit&&i==up);
	}
	if(!limit) dp[pos][sta]=ans;
	return ans;
}
ll solve(ll x){
	int pos=0;
	while(x){
		a[++pos]=x%10;
		x/=10;
	}
	return dfs(pos,0,true);
}
int main(){
	ios::sync_with_stdio(false);
    cout.tie(NULL);
    int T;
    scanf("%d",&T);
    memset(dp,-1,sizeof(dp));
    while(T--){
    	ll a,b;
    	scanf("%I64d%I64d",&a,&b);
    	printf("%I64d\n",solve(b)-solve(a-1));
	}
	return 0;
}




 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值