Cod sursa(job #1726871)

Utilizator borcanirobertBorcani Robert borcanirobert Data 9 iulie 2016 11:48:43
Problema Cifre Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include <fstream>
#include <iostream>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;

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

const int MAXCIF = 11;
int a[MAXCIF], b[MAXCIF];
int comb[MAXCIF][MAXCIF];
int C, K;
int rez;
long double x;
int A, B;

void Read();
int Solve( int a[MAXCIF] );
int Variante( int l, int cn );

int main()
{
    int i, j;

    Read();

    if ( K == 0 )
    {
        fout << "1.0000" << '\n';
        fin.close();
        fout.close();
        return 0;
    }

    comb[0][0] = 1;
    comb[1][0] = comb[1][1] = 1;

    for ( i = 2; i < MAXCIF; i++ )
    {
        comb[i][0] = 1;
        for ( j = 1; j <= i; j++ )
            comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
    }

    j = 0;
    for ( i = 1; i <= a[0]; i++ )
        if ( a[i] == C ) j++;

    if ( j >= K ) rez++;

    rez += Solve(b);
    rez -= Solve(a);

   // fout << rez << '\n';

    x = rez;
    x++;

    x = ((double)rez) / ((double)( B - A + 1 ));

    fout << fixed << setprecision(15) << (x*10000) / 10000 << '\n';

    fin.close();
    fout.close();
    return 0;
}

void Read()
{
    char x;

    while ( fin.get(x) && x >= '0' && x <= '9' )
        a[++a[0]] = x - '0', A = ( A * 10 ) + ( x - '0' );

    while ( fin.get(x) && x >= '0' && x <= '9' )
        b[++b[0]] = x - '0', B = ( B * 10 ) + ( x - '0' );

    fin >> C >> K;
}

int Solve( int a[MAXCIF] )
{
    int i, j, cif = 0;
    int res = 0;

 //   if ( a[0] == 1 )
   //     fout << "DA";

    if ( C != 0 )
        res = Variante(a[0] - 1, K);
    else
    {
        for ( i = a[0] - 2; i >= 1; i-- )
            res = res + 9*Variante(i, K);
        if ( K == 0 )
            res++;
    }

    for ( i = 1; i < a[0]; i++ )
    {
        for ( j = 1; j < a[i]; j++ )
        {
            if ( j == C ) res += Variante(a[0] - i, K - cif - 1);
            else    res += Variante(a[0] - i, K - cif);
        }

        if ( a[i] == C ) cif++;
    }

    if ( cif >= K ) res += ( a[i] + 1 );
    else
        if ( cif == K - 1 && a[i] >= C )
            res++;

    return res;
}

int Variante( int l, int cn )
{
    int i, ret = 0;

    if ( l <= 0 )
        return 0;

    if ( cn <= 0 )
        return pow(10, l);

    for ( i = cn; i <= l; i++ )
        ret = ret + ( pow(9, l - i) * comb[l][i] );

    return ret;
}