Cod sursa(job #496496)

Utilizator dushmiMihai-Alexandru Dusmanu dushmi Data 29 octombrie 2010 13:16:50
Problema Cifre Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include<cstdio>
const int MC=1<<4;
int q,a,b,c,k,nrb,rez,v[MC],vd[MC];
void rastorn(int A[MC])
{
    int aux,st=0,dr=q+1;
    while(++st<--dr)
        aux=A[st], A[st]=A[dr], A[dr]=aux;
}
void reset(int A[MC])
{
    for(int i=0;i<MC;i++)
        A[i]=0;
}
void back(int poz,int lung)
{
    if(poz>lung)
    {
        if(nrb>=k)
        {
            int prod=1;
            for(int i=1;i<=lung;i++)
                if(vd[i]==1)
                    prod*=9;
            rez+=prod;
        }
        return;
    }
    for(int i=0;i<2;i++)
    {
        if(i==0)
            nrb++;
        vd[poz]=i;
        back(poz+1,lung);
        if(i==0)
            nrb--;
    }
}
void solve(int poz)
{
    if(poz>q)
        return;
    int x=0;
    int lim=v[poz];
    if(poz<q)
        lim--;
    for(int i=0;i<=lim;i++)
    {
        if(i==c)
            nrb++;
        back(1,q-poz);
        if(i==c)
            nrb--;
    }
    if(v[poz]==c)
        nrb++;
    solve(poz+1);
}
int pregen(int x)
{
    reset(v);
    q=0;
    nrb=0;
    while(x)
        v[++q]=x%10, x/=10;
    rastorn(v);
    solve(1);
}
int main()
{
    freopen("cifre.in","r",stdin);
    freopen("cifre.out","w",stdout);
    scanf("%d%d%d%d",&a,&b,&c,&k);
    pregen(b);
    int x;
    x=rez;
    rez=0;
    pregen(a-1);
    x-=rez;
    printf("%.4f",(double)x/(double)(b-a+1));
    return 0;
}