rkyrahulkumar's blog

By rkyrahulkumar, history, 3 years ago, In English

https://www.codechef.com/problems/DGTCNT?tab=statement

can anyone suggest solution for this question?

include <bits/stdc++.h>

using namespace std; using ll = long long int;

ll f(ll i ,int skip,ll tight , vector a , string s){

if(i == s.size()){
        for(ll v : a) {
            if(v == 0) return 0;
        }
        return 1;
    }

    ll ans = 0;

    if(skip)
      ans += f(i+1,1,0 , a , s); //fill with 0 

    ll up = tight ? s[i] - '0' : 9;

    for(ll dig = skip ? 1 : 0 ; dig <= up ; dig++){
        a[dig]--;
        ans += f(i+1 ,0,tight & (dig == up),a,s);
    }

    return ans;

}

void solve(){

string l,r; cin>>l>>r; ll n = 10;

vector a(n); vector tmp;

for (int i = 0; i < n; ++i) { cin>>a[i]; }

ll rr = f(0,1,1,a,r); ll lr = f(0,1,1,a,to_string(stoi(l)-1)); ll ans = rr — ll; ll len = stoi(r) — stoi(l) + 1;

cout<<len — ans ;

}

int main() {

int t;
cin>>t;
while(t--)
solve();

return 0; }

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By rkyrahulkumar, history, 3 years ago, In English

Chef loves integers, but of course not all of them. For some personal reason, he considers the integers a0, a1, ..., and a9 unlucky. If for an integer x, suppose there exists some digit i (0 ≤ i ≤ 9) which appears exactly ai times in the decimal presentation of x (without leading zeros), then Chef dislikes x. Otherwise Chef loves it.

Chef wants to count the number of the integers between L and R, both inclusive, which he loves. Can you please help him in this?

Input The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follow.

The first line of each test case contains two space separated integers L, R.

The second line will contain exactly 10 space separated integers, the i-th integer denotes ai-1.

Output For each test case, output an integer corresponding to the answer, in a new line.

Constraints 1 ≤ T ≤ 20 1 ≤ L ≤ R ≤ 1018 0 ≤ ai ≤ 18 Subtasks Subtask #1 (25 points)

1 ≤ L ≤ R ≤ 105 Subtask #2 (15 points)

ai = 0, for all i Subtask #3 (60 points)

1 ≤ L ≤ R ≤ 1018 Sample 1: Input Output 2 21 28 5 4 3 2 1 1 2 3 4 5 233 23333 2 3 3 3 3 2 3 3 3 3 6 19627

Explanation:

In example 1. Chef dislikes the integers 24 because the digit 4 appears exactly 1 time in 24, and a4 = 1. Similarly, he also dislikes the integer 25 because the digit 5 appears exactly 1 time in 25 and a5 = 1. These are the only two integers which Chef dislikes in the range [21,28]. Thus Chef loves the integers 21, 22, 23, 26, 27, and 28. Therefore the answer is 6.

PROBLEM LINK

Full text and comments »

  • Vote: I like it
  • -7
  • Vote: I do not like it