C. Classy Numbers

链接

[http://codeforces.com/contest/1036/problem/C]

题意

给你l,r,让你找在这个闭区间内位数不为0不超过3的个数,1<=l,r<=1e18

分析

用一个dfs函数递归把1到1e18所有满足条件的放到一个vector
然后对于每个查找区间,用upper_bound(ve.begin(),ve.end(),r)-lower_bound(ve.begin(),ve.end(),l)就是答案了
需要注意的是保存1e18这个数因为它是19位数

代码

#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
vector< ll > ve;
void dfs(ll len,ll cur,ll n){
	ve.push_back(cur);
	if(len==18) return;
	dfs( len+1, cur*10, n);
	if(n<3)
	for(ll i=1;i<10;i++)
	dfs( len+1, cur*10+i, n+1);
}
int main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	for(ll i=1;i<10;i++)
	dfs(1,i,1);
	ve.push_back(1e18);
	sort(ve.begin(),ve.end());
	ll t,l,r;
	//freopen("in.txt","r",stdin);
	cin>>t;
	while(t--){
		cin>>l>>r;
		cout<<upper_bound(ve.begin(),ve.end(),r)-lower_bound(ve.begin(),ve.end(),l)<<endl;
	} 
	return 0;
}
posted @ 2018-10-30 20:25  ChunhaoMo  阅读(197)  评论(0)    收藏  举报