Cod sursa(job #113178)

Utilizator pauldbPaul-Dan Baltescu pauldb Data 9 decembrie 2007 00:32:02
Problema Cifre Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <stdio.h>
#include <string>

#define maxn 15
#define maxl 10

double sol;
int A,B,cif,K;
int c[maxn][maxn][2];
int n,m;
int a[maxn],b[maxn];

void convert(int x,int a[],int &n)
{
	int aux,i;

	while (x)
	{
		a[++n]=x%10;
		x/=10;
	}

	for (i=1;i<=n/2;i++)
	{
		aux=a[i];
		a[i]=a[n-i+1];
		a[n-i+1]=aux;
	}
}

int check(int x)
{
	if (cif==0 && K<=1 && x==0) return 1;

	int rez=0;

	while (x)
	{
		rez+=(x%10)==cif;
		x/=10;
	}

	return rez>=K;
}

int solve(int a[],int n)
{
	memset(c,0,sizeof(c));

	int i,j,k,rez=0;

	if (cif!=0) c[0][0][1]=1;
	else {
			for (i=1;i<a[1];i++) c[1][0][0]++;
			c[1][0][1]=1;
		 }

	for (i=0;i<n;i++)
	  for (j=0;j<=n;j++)
	  {
		  for (k=0;k<maxl;k++) 
			  if (k==cif) c[i+1][j+1][0]+=c[i][j][0];
			  else c[i+1][j][0]+=c[i][j][0];
		 
		  for (k=0;k<a[i+1];k++)
			  if (k==cif) c[i+1][j+1][0]+=c[i][j][1];
			  else c[i+1][j][0]+=c[i][j][1];

		  if (a[i+1]==cif) c[i+1][j+1][1]+=c[i][j][1];
		  else c[i+1][j][1]+=c[i][j][1];
	  }

	c[1][1][0]=1;

	if (cif!=0)	
		for (j=K;j<=n;j++) 
		{
			rez+=c[n][j][0]; 
			rez+=c[n][j][1];
		}
	else for (i=1;i<=n;i++)
			for (j=K;j<=n;j++) 
			{
				rez+=c[i][j][0];
				rez+=c[i][j][1];
			}
			
	return rez;
}

int main()
{
	freopen("cifre.in","r",stdin);
	freopen("cifre.out","w",stdout);

	scanf("%d %d %d %d ",&A,&B,&cif,&K);

    if (A==0) for (;;);
	convert(A-1,a,n);
	convert(B,b,m);

	sol=1.0*(solve(b,m)-solve(a,n))/(B-A+1);

	printf("%.4lf\n",sol);

	return 0;
}