Educational Codeforces Round 50 (Rated for Div. 2) C. Classy Numbers(dfs+二分)

题目链接:http://codeforces.com/contest/1036/problem/C

题意:给你一个区间,求非零数字不超过3个的十进制数的个数。

思路:构造所有的符合条件的数放入vector,然后二分查找上下界。

#include 
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
vector  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;
}

 

你可能感兴趣的:(【Codeforces】,【二分】,【搜索】)