Cod sursa(job #2873240)

Utilizator DenisONIcBanu Denis Andrei DenisONIc Data 18 martie 2022 22:36:42
Problema Cifre Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 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];
                }
                // cout << i << ' ' << j << ' ' << dp2[0][j] << ' '  << dp2[1][j] << '\n';
            }
        }
        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(){
    cin >> a >> b >> k >> c;


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