Cod sursa(job #2875865)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 22 martie 2022 14:52:22
Problema Cifre Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <bits/stdc++.h>
 
using namespace std;
 
int a, b, k, c;
 
int get_count(int x){
  vector<int> cif;
  while (x != 0){
    cif.push_back(x % 10);
    x /= 10;
  }
  reverse(cif.begin(), cif.end());
  
  // 0 - egal
  // 1 - mai mic
  vector<int> dp[2];
  dp[0].resize(10);
  dp[1].resize(10);
  dp[0][0] = 1;
  for (auto it : cif){
    vector<int> dp2[2];
    dp2[0].resize(10);
    dp2[1].resize(10);
    for (int i=0;i<=9;i++) {
      int add = (i == c);
      for (int j=9;j>=0;j--){
        if (j - add < 0){
          continue;
        }
        if (i < it){
          dp2[1][j] += dp[1][j - add] + dp[0][j - add];
        } else if (i == it){
          dp2[0][j] += dp[0][j - add];
          dp2[1][j] += dp[1][j - add];
        } else {
          dp2[1][j] += dp[1][j - add];
        }
      }
    }
    dp[0] = dp2[0];
    dp[1] = dp2[1];
  }

  int ans = 0;
  for (int i=k;i<=9;i++){
    ans += dp[0][i] + dp[1][i];  
  }
  return ans;
}
 
int main(){
  freopen("cifre.in","r",stdin);
  freopen("cifre.out","w",stdout);
  cin >> a >> b >> c >> k;

  cout << fixed << setprecision(4) << (long double)(get_count(b) - get_count(a - 1)) / (b - a + 1) << '\n';
  return 0;
}