Cod sursa(job #3139374)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 27 iunie 2023 18:53:37
Problema Cifre Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <bits/stdc++.h>
using namespace std;
int dp[10][10], dp_N0[10][10], k, c;

int calculator(int x){
    int xc = x, nrcif = 0;
    while(xc){
        xc /= 10;
        nrcif++;
    }
    int sol = 0;
    for(int i = k; i < nrcif; i++)
        for(int j = k; j <= i; j++)
            sol += dp_N0[i][j];
    xc = x;
    vector <int> cifs;
    while(xc){
        cifs.push_back(xc % 10);
        xc /= 10;
    }
    reverse(cifs.begin(), cifs.end());
    int kc = k, first_digit = 1, index = (int) cifs.size() - 1;
    for(auto cif : cifs){
        for(int i = first_digit; i < cif + (index == 0); i++)
            for(int j = max(0, kc - (i == c)); j <= index; j++)
                sol += dp[index][j];
        first_digit = 0;
        index--;
        if(cif == c) kc--;
    }
    return sol;
}

int main() {
    int a, b, i, j;
    freopen("cifre.in", "r", stdin);
    freopen("cifre.out", "w", stdout);
    scanf("%d%d%d%d", &a, &b, &c, &k);
    dp[0][0] = 1;
    dp[1][0] = dp_N0[1][0] = 9;
    dp[1][1] = dp_N0[1][1] = 1;
    for(i = 1; i <= 8; i++){
        for(j = 0; j <= i; j++) {
            dp[i + 1][j] += 9 * dp[i][j];
            dp[i + 1][j + 1] += dp[i][j];
            if (c == 0) {
                dp_N0[i + 1][j] += 9 * dp[i][j];
                dp_N0[i + 1][j + 1] += 0;
            } else {
                dp_N0[i + 1][j] += 8 * dp[i][j];
                dp_N0[i + 1][j + 1] += dp[i][j];
            }
        }
    }
    printf("%.4f\n", 1.0 * (calculator(b) - calculator(a - 1)) / (b - a + 1));

    return 0;
}