Cod sursa(job #720332)

Utilizator costyv87Vlad Costin costyv87 Data 22 martie 2012 16:09:15
Problema Cifre Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.99 kb
#include <cstdio>
#include <algorithm>
#include <math.h>
using namespace std;
FILE *f,*g;
int a,b,c,k,c1,c2,i,j,sol,r1,s2,s1,s,sa,sb;
double t;
int p[25][25];
int st[20];

void pre() {
int x;
for (x=a;x;x/=10) c1++;
for (x=b;x;x/=10) c2++;
s2=b-a+1;
s1=0;
for (i=0;i<=20;i++) p[i][0]=1;
for (i=1;i<=20;i++) 
		for (j=1;j<=20;j++)
			p[i][j]=p[i-1][j]+p[i-1][j-1];
					
}

int df(int z) {
int r,s=0,cn,j,ax;
if (z>st[0]) {
	for (j=1,ax=0;j<z;j++) if (st[j]==c) ax++;
	if (ax<k) 
		return 1;
	else 
		return 0;
	}
	
if (z==1) {
	if (c==0){
		for (j=0;j<=min(k-1,st[0]-1);j++) 
			s=s+p[st[0]-1][j]*(st[1]-1>=0?(st[1]-1):0)*pow(9,st[0]-1-j);
		}
	else {
		if (c<st[1]) cn=1; else cn=0;
		for (j=0;j<=min(k-1,st[0]-1);j++)     // Cand fixam prima poztie diferita de c
			s=s+p[st[0]-1][j]*(st[1]-cn-1)*pow(9,st[0]-1-j);
		if (cn==1)
			for (j=0;j<=min(k-2,st[0]-1);j++) // cand o fixam exact pe c, daca se poate(cn==1)
				s=s+p[st[0]-1][j]*pow(9,st[0]-1-j);
		}
	}
else {
	ax=0; //Numarul de cifre c din spate 
	for (j=1;j<z;j++) if (st[j]==c) ax++;
	if (ax>=k) return 0;
	
	if (c==0) {
		for (j=0;j<=min(k-1-ax,st[0]-z);j++)  
			s=s+p[st[0]-z][j]*(st[z]-1>=0?st[z]-1:0)*pow(9,st[0]-z-j);
		if(st[z]>0)
			for (j=0;j<=min(k-2-ax,st[0]-z);j++)
				s=s+p[st[0]-z][j]*pow(9,st[0]-z-j);
		}
	else {
		if (c<st[z]) cn=1; else cn=0;
		for (j=0;j<=min(k-1-ax,st[0]-z);j++)    // Cand fixam pozitia z diferita de c
			s=s+p[st[0]-z][j]*(st[z]-cn)*pow(9,st[0]-z-j);
		if (cn==1) 
			for (j=0;j<=min(k-2-ax,st[0]-1);j++) // Cand fixam pozitia z exact pe c, daca se poate (cn==1)
				s=s+p[st[0]-z][j]*pow(9,st[0]-z-j);
		}
	}
		

return s+df(z+1);
}

inline int ok(int x) {
int ax=0;

while (x!=0)  {
	if (x%10==c) ax++;
	x/=10;
	}
if (ax>=k) return 1;
return 0;
}


inline void inv() {
int i;
for (i=1;i<=st[0]/2;i++) 
	swap(st[i],st[st[0]-i+1]);
}

void solve() {
int x;

if (c==0) {r1=9;}  else { r1=8; }
sa=sb=0;



for (i=1;i<c1;i++) {
	s=0;
	if (c==0) {
		for (j=0;j<=min(k-1,i-1);j++) 
			s=s+p[i-1][j]*9*pow(9,i-1-j);
		}
	else {
		for (j=0;j<=min(k-1,i-1);j++)
			s=s+p[i-1][j]*8*pow(9,i-1-j);
		for (j=0;j<=min(k-2,i-1);j++) 
			s=s+p[i-1][j]*pow(9,i-1-j);
		}
	sa=sa+( (9*pow(10,i-1))-s);
	}
for (i=1;i<c2;i++) {
	s=0;
	if (c==0) {
		for (j=0;j<=min(k-1,i-1);j++) 
			s=s+p[i-1][j]*9*pow(9,i-1-j);
		}
	else {
		for (j=0;j<=min(k-1,i-1);j++)
			s=s+p[i-1][j]*8*pow(9,i-1-j);
		for (j=0;j<=min(k-2,i-1);j++) 
			s=s+p[i-1][j]*pow(9,i-1-j);
		}
	sb=sb+( (9*pow(10,i-1))-s);
	}
if (c==0 && k<=1) { sa++; sb++; }

for (st[0]=0,x=a;x;) { st[++st[0]]=x%10; x/=10; }
inv();
sa=sa+ (a-pow(10,st[0]-1)+1-df(1)); 
for (st[0]=0,x=b;x;) { st[++st[0]]=x%10; x/=10; } 
inv();
sb=sb+(b-pow(10,st[0]-1)+1-df(1)); 



t=(double) (sb-sa+ok(a))/(b-a+1);
fprintf(g,"%.4lf",t);
}
	
	

int main() {
f=fopen("cifre.in","r");
g=fopen("cifre.out","w");

fscanf(f,"%d%d%d%d",&a,&b,&c,&k);

pre();
solve();

fclose(g);
return 0;
}