C. Classy Numbers

链接

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

题意

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

分析

用一个dfs函数递归把1到1e18所有满足条件的放到一个vector

代码

#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;
}

猜你喜欢

转载自www.cnblogs.com/mch5201314/p/9879309.html