Cod sursa(job #2207057)

Utilizator Andrei_CotorAndrei Cotor Andrei_Cotor Data 24 mai 2018 20:01:55
Problema Cifre Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.99 kb
#include<fstream>
#include<iomanip>
using namespace std;
ifstream fi("cifre.in");
ofstream fo("cifre.out");
int X[15],A,B,c,k,Dp[15][15][15][2],i,j,ind,cif,b;
long double rez;

int f(int x)
{
    int n=0;
    while(x)
    {
        X[++n]=x%10;
        x/=10;
    }
    for(i=0; i<=n+1; i++)
        for(j=0; j<=n; j++)
            for(ind=0; ind<=9; ind++)
                for(b=0; b<=1; b++)
                    Dp[i][j][ind][b]=0;
    int nj,nb;
    for(i=1; i<=X[n]; i++)
    {
        if(i==c)
            nj=1;
        else
            nj=0;
        if(i==X[n])
            nb=0;
        else
            nb=1;
        Dp[n][nj][i][nb]=1;
    }
    for(i=1; i<n; i++)
    {
        for(j=1; j<=9; j++)
        {
            if(j==c)
                nj=1;
            else
                nj=0;
            Dp[i][nj][j][1]=1;
        }
    }
    for(i=n; i>=2; i--)
    {
        for(j=0; j<=n; j++)
        {
            for(ind=0; ind<=9; ind++)
            {
                for(b=0; b<=1; b++)
                {
                    for(cif=0; cif<=9; cif++)
                    {
                        if(b==0 && cif>X[i-1])
                            continue;
                        if(Dp[i][j][ind][b]==0)
                            continue;
                        if(cif==c)
                            nj=j+1;
                        else
                            nj=j;
                        if(cif<X[i-1] || b==1)
                            nb=1;
                        else
                            nb=0;
                        Dp[i-1][nj][cif][nb]+=Dp[i][j][ind][b];
                    }
                }
            }
        }
    }
    int rez=0;
    for(i=0; i<=9; i++)
        for(j=k; j<=n; j++)
            rez+=Dp[1][j][i][1];
    return rez;
}

int main()
{
    fi>>A>>B>>c>>k;
    rez=f(B+1)-f(A);
    fo<<setprecision(4)<<fixed<<rez/(B-A+1)<<"\n";
    fi.close();
    fo.close();
    return 0;
}