Cod sursa(job #2630721)

Utilizator PetyAlexandru Peticaru Pety Data 26 iunie 2020 20:36:00
Problema Cifre Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("cifre.in");
ofstream fout ("cifre.out");

int a, b, c , k, v[12], dp[12][12][10][2];

int nr (int x) {
  memset(dp, 0, sizeof(dp));
  int cp = x;
  int n = 0;
  while (cp) {
    v[++n] = cp % 10;
    cp /= 10;
  }
  for (int cif = 1; cif <= v[n]; cif++){
    if (cif == c)
      dp[n][1][cif][v[n] > cif ? 0 : 1] = 1;
    else
      dp[n][0][cif][v[n] > cif ? 0 : 1] = 1;
  }
  for (int i = 1; i < n; i++)
    for (int j = 1; j <= 9; j++)
      dp[i][j == c][j][0] = 1;
  for (int i = n; i >= 2; i--)
    for (int j = 0; j <= n; j++)
      for (int cif = 0; cif <= 9; cif++)
        for (int p = 0; p < 2; p++)
          for (int newCif = 0; newCif < 10; newCif++) {
            if ((p == 1 && v[i - 1] < newCif) || !dp[i][j][cif][p])
              continue;
            bool p1 = 1;
            if (newCif < v[i - 1] || p == 0)
              p1 = 0;
            dp[i - 1][j + (newCif == c)][newCif][p1] += dp[i][j][cif][p];
          }
  int ans = 0;
  for (int i = 0; i <= 9; i++)
    for (int j = k; j <= n; j++)
      ans += dp[1][j][i][0] + dp[1][j][i][1];
  return ans;
}

int main()
{
  fin >> a >> b >> c >> k;
  int ans = nr(b) - nr(a - 1);
  //fout << ans;
  fout << fixed << setprecision(4) << (double)ans / (b - a + 1);
  return 0;
}