Cod sursa(job #1016294)

Utilizator nicuvladNicu Vlad nicuvlad Data 26 octombrie 2013 00:08:22
Problema Cifre Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

int DP[15][15][2][2], A, B, C, K, V[15], N, P;

int Solve(int Limit)
{
    memset(DP, 0, sizeof(DP));

    P = 0;
    while(Limit) V[P ++] = Limit % 10, Limit /= 10;
    reverse(V, V + P);

    N = P;
    DP[0][0][1][0] = 1;

    for(int i = 0; i < N; ++ i)
        for(int j = 0; j < N; ++ j)
            for(int k = 0; k < 2; ++ k)
                for(int l = 0; l < 2; ++ l)
                    for(int d = 0; d <= 9; ++ d)
                    {
                        int _i, _j, _k, _l;
                        if(k == 1 && d > V[i]) continue;

                        _i = i + 1;

                        if(d == C && (C != 0 || l == 1)) _j = j + 1;
                        else _j = j;

                        if(k == 1 && d == V[i]) _k = 1;
                        else _k = 0;

                        if(l == 0 && d == 0) _l = 0;
                        else _l = 1;

                        DP[_i][_j][_k][_l] += DP[i][j][k][l];
                    }

    int Ans = 0;
    for(int i = K; i <= N; ++ i)
        Ans += DP[N][i][0][1] + DP[N][i][1][1];
    if(C == 0) Ans ++;
    return Ans;
}

int main()
{
    freopen("cifre.in", "r", stdin);
    freopen("cifre.out", "w", stdout);

    scanf("%i %i %i %i", &A, &B, &C, &K);
    printf("%f\n", 1.0 * (Solve(B) - Solve(A - 1)) / (B - A + 1));

    return 0;
}