Cod sursa(job #2149681)

Utilizator ovidius11Stiriu Ovidius ovidius11 Data 2 martie 2018 21:06:17
Problema Cifre Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include<cstdio>
#include<cmath>
int p10[10];
int cc(int nr){
return (int)log10((double)nr)+1;}
int cif(int poz,int nr){
return nr/p10[poz-1]%10;}
int calc(int l,int c,int k){
if (k==0)
return p10[l];
if (l==0)
return 0;
return 9*calc(l-1,c,k)+calc(l-1,c,k-1);}
int calcint(int a,int b,int c,int k,int poz){
if (k==0)
return b-a+1;
if (poz==0)
return 0;
if (poz==1)
if (cif(poz,a)<=c && c<=cif(poz,b) && k==1)
return 1;
else
return 0;
if (cif(poz,a)==cif(poz,b)){
if (c==0){
if (poz<=cc(a) && cif(poz,a)==c)
return calcint(a,b,c,k-1,poz-1);
else
return calcint(a,b,c,k,poz-1);}
else{
if (cif(poz,a)==c)
return calcint(a,b,c,k-1,poz-1);
else
return calcint(a,b,c,k,poz-1);}}
if (cif(poz,b)==cif(poz,a)+1){
return calcint(a,cif(poz,a)*p10[poz-1]+p10[poz-1]-1,c,k,poz)+calcint(cif(poz,a)*p10[poz-1]+p10[poz-1],b,c,k,poz);}
else{
int nr=calcint(a,cif(poz,a)*p10[poz-1]+p10[poz-1]-1,c,k,poz)+calcint(cif(poz,b)*p10[poz-1],b,c,k,poz);
int cif1=cif(poz,a)+1,cif2=cif(poz,b)-1;
if (cif1<=c && c<=cif2)
nr=nr+(cif2-cif1)*calc(poz-1,c,k)+calc(poz-1,c,k-1);
else
nr=nr+(cif2-cif1+1)*calc(poz-1,c,k);
return nr;}}
int main(){
freopen("cifre.in","r",stdin);
freopen("cifre.out","w",stdout);
int a,b,c,k,i;
scanf("%d%d%d%d",&a,&b,&c,&k);
p10[0]=1;
for(i=1;i<=10;i++)
p10[i]=p10[i-1]*10;
int nr=calcint(a,b,c,k,9);
printf("%.4f\n",(double)nr/(double)(b-a+1));
return 0;}