dfs打表 C. Classy Numbers
C. Classy Numbers
C. Classy Numbers time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard outputLet‘s call some positive integer classy if its decimal representation contains no more than 33 non-zero digits. For example, numbers 44, 200000200000, 1020310203 are classy and numbers
You are given a segment [L;R][L;R]. Count the number of classy integers xx such that L≤x≤RL≤x≤R.
Each testcase contains several segments, for each of them you are required to solve the problem separately.
InputThe first line contains a single integer TT (
Each of the next TT lines contains two integers LiLi and RiRi (1≤Li≤Ri≤10181≤Li≤Ri≤1018).
OutputPrint TT lines — the ii-th line should contain the number of classy integers on a segment [Li;Ri][Li;Ri].
Example input Copy4
1 1000
1024 1024
65536 65536
999999 1000001
1000
1
0
2
題意:找出區間[L, R]之間最多有三位是非零數字的十進制數的個數;
思路:用構造出所有的滿足題意的數,然後排序,二分找;
也可數位DP
/***********************************************/ ll a,b,n; vector<ll>V; void dfs(ll x,int oo,int len) { V.push_back(x); if(len==18) return ; dfs(x*10,oo,len+1); if(oo<3) for(int i=1;i<=9;i++) dfs(x*10+i,oo+1,len+1); } int main() { for(ll i=1;i<=9;i++) { dfs(i,1,1); } V.push_back(1e18); sort(V.begin(),V.end()); scl(n); while(n--) { ll ans=0; scl2(a,b); ll l=lower_bound(V.begin(),V.end(),a)-V.begin(); ll r=upper_bound(V.begin(),V.end(),b)-V.begin(); cout<<r-l<<endl; } return 0; }
dfs打表 C. Classy Numbers