Pagini recente » Cod sursa (job #49880) | Cod sursa (job #52551) | Cod sursa (job #1423242) | Cod sursa (job #2566100) | Cod sursa (job #496488)
Cod sursa(job #496488)
#include<cstdio>
const int MC=1<<4;
int q,a,b,c,k,nrb,v[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;
}
int din(int x,int lung)
{
int a[MC][MC];
for(int i=0;i<MC;i++)
reset(a[i]);
if(lung==q && c==0)
{
a[1][0]=x+1;
}
else
{
if(x>=c)
{
a[1][0]=x;
a[1][1]=1;
}
else
{
a[1][0]=x+1;
a[1][1]=0;
}
}
for(int i=2;i<=lung;i++)
{
a[i][0]=9*a[i-1][0];
for(int j=1;j<=lung;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j]*9;
}
int rez=0;
int j=0;
if(k-nrb>0)
j=k-nrb;
else j=0;
for(j=j;j<=lung;j++)
rez+=a[lung][j];
return rez;
}
int solve(int poz)
{
if(poz>q)
return 0;
int x=0;
if(poz<q)
x=din(v[poz]-1,q-poz+1);
else
x=din(v[poz],q-poz+1);
if(v[poz]==c)
nrb++;
return x+solve(poz+1);
}
int pregen(int x)
{
reset(v);
q=0;
nrb=0;
while(x)
v[++q]=x%10, x/=10;
rastorn(v);
return solve(1);
}
int main()
{
freopen("cifre.in","r",stdin);
freopen("cifre.out","w",stdout);
scanf("%d%d%d%d",&a,&b,&c,&k);
int x=pregen(b)-pregen(a-1);
printf("%.4f",(double)x/(double)(b-a+1));
return 0;
}