本文主要是介绍Educational Codeforces Round 50 (Rated for Div. 2) C. Classy Numbers(dfs+二分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:http://codeforces.com/contest/1036/problem/C
题意:给你一个区间,求非零数字不超过3个的十进制数的个数。
思路:构造所有的符合条件的数放入vector,然后二分查找上下界。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
vector <ll> v;void dfs(ll x, int cnt, int len)
{if (cnt == 4)return;if (len == 18){v.push_back(x);return;}for (int i = 0; i <= 9; ++i)dfs(x * 10 + i, cnt + (i != 0), len + 1);
}
int main()
{IOS; int t; ll l, r;dfs(0, 0, 0);v.push_back(1e18);sort(v.begin(), v.end());cin >> t;while (t--){cin >> l >> r;cout << (upper_bound(v.begin(), v.end(), r) - lower_bound(v.begin(), v.end(), l)) << endl;}return 0;
}
这篇关于Educational Codeforces Round 50 (Rated for Div. 2) C. Classy Numbers(dfs+二分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!