CF-Educational Codeforces Round 50 (Rated for Div. 2) -C. Classy Numbers

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/C_13579/article/details/82531151

地址:http://codeforces.com/contest/1036/problem/C

思路:开始是想直接求1->L和1->R的个数,结果分析起来很麻烦,后来看别人的代码分析是先直接将所有的满足条件的数全部找出来。。。还是太差了QAQ

Code:

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;

int Q;
LL l,r;
vector<LL> a;

void Find(LL cur,int s);
int main()
{
	ios::sync_with_stdio(false);
	for(int i=1;i<=9;++i)
		Find(i,1);
	sort(a.begin(),a.end());
	cin>>Q;
	while(Q--){
		cin>>l>>r;
		cout<<upper_bound(a.begin(),a.end(),r)-lower_bound(a.begin(),a.end(),l)<<endl;
	}
	
	return 0;
}

void Find(LL cur, int s)
{
	a.push_back(cur);
	while(cur<=1e17){
		cur*=10;
		a.push_back(cur);
		if(s<3){
			for(int i = 1; i < 10; i++)
				Find(cur + i, s + 1);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/C_13579/article/details/82531151