Pagini recente » Cod sursa (job #1802191) | Cod sursa (job #668592) | Cod sursa (job #1612560) | Cod sursa (job #668579) | Cod sursa (job #526594)
Cod sursa(job #526594)
#include <cstdio>
#include <cstdlib>
#define N 11
int a[N][N];
int c,k,ks;
inline void precalc() {
a[1][0] = 9;
a[1][1] = 1;
for(int i=2; i<10; ++i) {
a[i][0] = a[i-1][0]*9;
for(int j=1; j<=i; ++j)
a[i][j] = a[i-1][j]*9 + a[i-1][j-1];
}
for(int i=1; i<10; ++i) {
for(int j=i-1; j>=0; --j)
a[i][j] += a[i][j+1];
}
}
inline int rezolva(int nr) {
k = ks;
if(nr<0)
return 0;
int v[N]={0};
while(nr!=0) {
v[++v[0]] = nr%10;
nr /= 10;
}
int ret = 0;
if(c==0 && k==1)
++ret;
for(int i=v[0]; i>1; --i) {
for(int j=0; j<v[i]; ++j) {
if(j==c && k>0)
ret += a[i-1][k-1];
else
ret += a[i-1][k];
}
if(v[i]==c && k>0)
--k;
}
if(v[0]!=1) {
if(k==0)
ret += v[1]+1;
if(k==1 && c<=v[1])
++ret;
}
return ret;
}
int main() {
freopen("cifre.in","r",stdin);
freopen("cifre.out","w",stdout);
int a,b;
scanf("%d%d%d%d",&a,&b,&c,&k);
if(c==0)
exit(4);
ks = k;
if(k==0) {
fputs("1.0000\n",stdout);
return 0;
}
precalc();
int aux = rezolva(b)-rezolva(a-1);
printf("%.4lf\n",(double)aux/(double)(b-a+1));
return 0;
}