C. Classy Numbers -cocdeforces1036c -csdn博客

C. Classy Numbers -cocdeforces1036c -csdn博客

Let’s call some positive integer classy if its decimal representation contains no more than 3 non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000 are not.

You are given a segment [L;R]. Count the number of classy integers x such that L≤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 T (1≤T≤10e4) — the number of segments in a testcase.
Each of the next T lines contains two integers Li and Ri (1≤Li≤Ri≤10e18).
Output
Print T lines — the i-th line should contain the number of classy integers on a segment [Li;Ri].

Example
input
4
1 1000
1024 1024
65536 65536
999999 1000001
output
1000
1
0
2

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

  • 题解:数位dp,直接套数位dp的模板即可

#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define mp make_pair
#define _ ios::sync_with_stdio(false)
bool SUBMIT = 1;
typedef long long ll;
using namespace std;
const double PI = acos(-1);
ll n,k[20],sn[20][2][4];
ll dp(ll l,int up,ll check)//up用来判断是否属于是否处于上界,
{//check判断已经有了几个这样的数
	if(l<0)return 1;
	ll c=sn[l][up][check];
	if(c)return c;
	ll ans=0;
	if(check>=3)return sn[l][up][check]=1;
	ll m;
	if(up)m=k[l];
	else m=9;
	for(int i=0;i<=m;i++)
	{
		ans+=dp(l-1,up&&(i==m),check+(i==0?0:1) );
	}
	return sn[l][up][check]=ans;
}
ll fun(ll m)
{
	ll l=0;
	memset(sn,0,sizeof(sn));
	while(m>0)//把整数分解
	{
		k[l++]=m%10;
		m/=10;
	}
	return dp(l-1,1,0);
}
int main()
{
	if(!SUBMIT)freopen("i.txt","r",stdin);else _;
	cin>>n;
	while(n--)
	{
		ll a,b;cin>>a>>b;
		cout<<fun(b)-fun(a-1);<<endl;
	}
	return 0;
}
posted @ 2018-09-10 20:37 i-Curve 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值