Pagini recente » Cod sursa (job #1735056) | Cod sursa (job #2587891) | Cod sursa (job #1277482) | Cod sursa (job #1766468) | Cod sursa (job #1730640)
#include <cstdio>
#define MAXCIF 10
int c,k;
int cif[MAXCIF+1],fact[MAXCIF+1],p9[MAXCIF+1];
inline int comb(int n,int k){
return fact[n]/(fact[k]*fact[n-k]);
}
inline int cauta(int x){
int i,nrcif,rez,f,p,j;
if(x==-1)
return 0;
if(x==0&&c==0&&k<=1)
return 1;
nrcif=0;
while(x>0){
cif[nrcif++]=x%10;
x/=10;
}
rez=0;
i=nrcif-1;
x=0;
while(i>=0){
for(p=cif[i]-1;p>=0;p--){
if(c>0||(c==0&&i<nrcif-1)){
if(p==c)
f=1;
else
f=0;
j=k-x-f;
if(j<0)
j=0;
while(j<=i){
rez+=comb(i,j)*p9[i-j];
j++;
}
}
else
rez++;
}
if(cif[i]==c)
x++;
i--;
}
return rez;
}
int main(){
FILE*fi,*fout;
int a,b,i;
double rez1,rez2;
fi=fopen("cifre.in" ,"r");
fout=fopen("cifre.out" ,"w");
fscanf(fi,"%d%d%d%d" ,&a,&b,&c,&k);
fact[0]=1;
p9[0]=1;
for(i=1;i<=MAXCIF;i++){
fact[i]=fact[i-1]*i;
p9[i]=p9[i-1]*9;
}
rez1=cauta(b+1)-cauta(a-1);
rez2=b-a+1;
fprintf(fout,"%.4lf" ,rez1/rez2);
fclose(fi);
fclose(fout);
return 0;
}