Cod sursa(job #35214)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 21 martie 2007 22:01:03
Problema Cifre Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2 kb
# include <stdio.h>

const int MAXLEN=9,MAXCF=10,MAXK=10;
long int nr[MAXLEN+1][MAXCF+1][MAXK+1],a,b,c,k;

void citire()
{
FILE *f=fopen("cifre.in","r");
fscanf(f,"%ld%ld%ld%ld",&a,&b,&c,&k);
fclose(f);
}

void test_c();

void compute()
{
long int len,first,rep,j;
////////////////////
//  INIT
////////////////////
for (first=0;first<=9;first++)
	if (first==c) nr[1][first][1]=1;
	else nr[1][first][0]=1;
////////////////////
//  COMPUTE THE REST
////////////////////
for (len=2;len<=9;len++)
for (first=0;first<=9;first++)
for (rep=0;rep<=len;rep++)
	if (first==c)
		{
		if (rep-1>=0)
		for (j=0;j<=9;j++) nr[len][first][rep]+=nr[len-1][j][rep-1];
		}
	else
		for (j=0;j<=9;j++) nr[len][first][rep]+=nr[len-1][j][rep];
//test_c();
}

long int sol(long int num, long int nec);

long int solutie(long int num)
{
long int nec=k,l=0,aux=num,s=0;
if (num==0) return 0;
if (num==-1) return 0;
while (aux) {l++;aux/=10;}
for (nec=k;nec<=l;nec++)
	s+=sol(num,nec);
return s;
}

long int sol(long int num,long int nec)
{
long int p=1,l=1,li;
while (p*10<=num) {p*=10;l++;}
long int total=0,q=0,j,fr;li=l;
if (c==0)
	for (j=l-1;j>=1;j--)
	for (fr=1;fr<=9;fr++) total+=nr[j][fr][nec];
do
	{
	q=0;if (li==l&&c==0) q=1;
	while (q<num/p)
		{
		total+=nr[l][q][nec];
		q++;
		}
	if (num/p==c&&nec>0) nec--;
	while (num/p) num-=p;
	//if (l==1&&num==0&&nec==0) {total+=1;break;}
	p/=10;
	l--;
	if (l&&num%p==0)
		{
		if (num/p!=0&&num/p==c&&nec==1) total+=1;
		if (num/p!=c&&nec==0) total+=1;
		if (c==0&&nec==l-1) total+=1;
		break;
		}
	}
while (l);
return total;
}

void scrie()
{
FILE *g=fopen("cifre.out","w");
fprintf(g,"%-.4f\n",((float)solutie(b)-solutie(a-1))/(b-a+1));
fcloseall();
}

int main()
{
citire();
compute();
scrie();
return 0;
}

void test_c()
{
int len,first,rep,cc;
scanf("%d",&cc);
scanf("%d%d%d",&len,&first,&rep);
while (len)
	{
	printf("Solutia este: %ld\n",nr[len][first][rep]);
	scanf("%d%d%d",&len,&first,&rep);
	}
}