Cod sursa(job #300460)

Utilizator shnakoVlad Schnakovszki shnako Data 7 aprilie 2009 14:14:31
Problema Cifre Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.82 kb
#include <stdio.h>
#define NR 10
//#error nra si nrb nu merg calumea
FILE *f=fopen("cifre.in", "r"), *g=fopen("cifre.out", "w");
long n, j, i, a, b, c, k, sw, total, t, x[10][10], nrb, nra, s, left, right, z[10][10];

void preprocesare(void)
{
x[1][0]=8;
x[1][1]=1;
x[1][2]=0;
x[1][3]=0;
x[1][4]=0;
x[1][5]=0;
x[1][6]=0;
x[1][7]=0;
x[1][8]=0;
x[1][9]=0;
x[2][0]=80;
x[2][1]=18;
x[2][2]=1;
x[2][3]=0;
x[2][4]=0;
x[2][5]=0;
x[2][6]=0;
x[2][7]=0;
x[2][8]=0;
x[2][9]=0;
x[3][0]=728;
x[3][1]=243;
x[3][2]=27;
x[3][3]=1;
x[3][4]=0;
x[3][5]=0;
x[3][6]=0;
x[3][7]=0;
x[3][8]=0;
x[3][9]=0;
x[4][0]=6560;
x[4][1]=2916;
x[4][2]=486;
x[4][3]=36;
x[4][4]=1;
x[4][5]=0;
x[4][6]=0;
x[4][7]=0;
x[4][8]=0;
x[4][9]=0;
x[5][0]=59048;
x[5][1]=32805;
x[5][2]=7290;
x[5][3]=810;
x[5][4]=45;
x[5][5]=1;
x[5][6]=0;
x[5][7]=0;
x[5][8]=0;
x[5][9]=0;
x[6][0]=531440;
x[6][1]=354294;
x[6][2]=98415;
x[6][3]=14580;
x[6][4]=1215;
x[6][5]=54;
x[6][6]=1;
x[6][7]=0;
x[6][8]=0;
x[6][9]=0;
x[7][0]=4782968;
x[7][1]=3720087;
x[7][2]=1240029;
x[7][3]=229635;
x[7][4]=25515;
x[7][5]=1701;
x[7][6]=63;
x[7][7]=1;
x[7][8]=0;
x[7][9]=0;
x[8][0]=43046720;
x[8][1]=38263752;
x[8][2]=14880348;
x[8][3]=3306744;
x[8][4]=459270;
x[8][5]=40824;
x[8][6]=2268;
x[8][7]=72;
x[8][8]=1;
x[8][9]=0;
x[9][0]=387420488;
x[9][1]=387420489;
x[9][2]=172186884;
x[9][3]=44641044;
x[9][4]=7440174;
x[9][5]=826686;
x[9][6]=61236;
x[9][7]=2916;
x[9][8]=81;
x[9][9]=1;
z[1][0]=9;
z[1][1]=1;
z[1][2]=0;
z[1][3]=0;
z[1][4]=0;
z[1][5]=0;
z[1][6]=0;
z[1][7]=0;
z[1][8]=0;
z[1][9]=0;
z[2][0]=81;
z[2][1]=9;
z[2][2]=0;
z[2][3]=0;
z[2][4]=0;
z[2][5]=0;
z[2][6]=0;
z[2][7]=0;
z[2][8]=0;
z[2][9]=0;
z[3][0]=729;
z[3][1]=162;
z[3][2]=9;
z[3][3]=0;
z[3][4]=0;
z[3][5]=0;
z[3][6]=0;
z[3][7]=0;
z[3][8]=0;
z[3][9]=0;
z[4][0]=6561;
z[4][1]=2187;
z[4][2]=243;
z[4][3]=9;
z[4][4]=0;
z[4][5]=0;
z[4][6]=0;
z[4][7]=0;
z[4][8]=0;
z[4][9]=0;
z[5][0]=59049;
z[5][1]=26244;
z[5][2]=4374;
z[5][3]=324;
z[5][4]=9;
z[5][5]=0;
z[5][6]=0;
z[5][7]=0;
z[5][8]=0;
z[5][9]=0;
z[6][0]=531441;
z[6][1]=295245;
z[6][2]=65610;
z[6][3]=7290;
z[6][4]=405;
z[6][5]=9;
z[6][6]=0;
z[6][7]=0;
z[6][8]=0;
z[6][9]=0;
z[7][0]=4782969;
z[7][1]=3188646;
z[7][2]=885735;
z[7][3]=131220;
z[7][4]=10935;
z[7][5]=486;
z[7][6]=9;
z[7][7]=0;
z[7][8]=0;
z[7][9]=0;
z[8][0]=43046721;
z[8][1]=33480783;
z[8][2]=11160261;
z[8][3]=2066715;
z[8][4]=229635;
z[8][5]=15309;
z[8][6]=567;
z[8][7]=9;
z[8][8]=0;
z[8][9]=0;
z[9][0]=387420489;
z[9][1]=344373768;
z[9][2]=133923132;
z[9][3]=29760696;
z[9][4]=4133430;
z[9][5]=367416;
z[9][6]=20412;
z[9][7]=648;
z[9][8]=9;
z[9][9]=0;
}




void citeste(void)
	{
   fscanf(f, "%ld%ld%ld%ld", &a, &b, &c, &k);
	total=b-a+1;
   }


void rezolvax(void)
	{
   t=b;
   nrb=0;
   while (t)
   	{
      nrb++;
      t*=0.1;
      }
	t=a;
   nra=0;
   while (t)
   	{
      nra++;
      t*=0.1;
      }
      left=1;
     	for (i=1;i<nra;i++)
     		left*=10;
      right=1;
      for (i=1;i<=nrb;i++)
      	right*=10;
      for (i=nra;i<=nrb;i++)
 	   	for (j=k;j<=9;j++)
     			s=s+x[i][j];
      for (i=1;i<nra;i++)
      	s=s-x[nra-1][i];
      if (10*b==right)
      	s--;

      for (i=left;i<a;i++)
      	{
         sw=0;
         t=i;
         while (t)
         	{
            if (t%10==c)
            	sw++;
            t*=0.1;
         	}
         if (sw>=k)
            s--;
         }
   	for (i=b+1;i<right;i++)
      	{
         sw=0;
         t=i;
         while (t)
         	{
            if (t%10==c)
            	sw++;
            t*=0.1;
            }
         if (sw>=k)
         	s--;
      	}
   }

void rezolvaz(void)
	{
   t=b;
   nrb=0;
   while (t)
   	{
      nrb++;
      t*=0.1;
      }
	t=a;
   nra=0;
   while (t)
   	{
      nra++;
      t*=0.1;
      }
      left=1;
     	for (i=1;i<nra;i++)
     		left*=10;
      for (i=nra;i<=nrb;i++)
 	   	for (j=k;j<=9;j++)
     			s=s+z[i][j];
      for (i=1;i<nra;i++)
      	s=s-z[nra-1][i];
      s++;

      for (i=left;i<a;i++)
      	{
         sw=0;
         t=i;
         while (t)
         	{
            if (t%10==c)
            	sw++;
            t*=0.1;
         	}
         if (sw>=k)
            s--;
         }
      right=1;
      for (i=1;i<=nrb;i++)
      	right*=10;
   	for (i=b+1;i<right;i++)
      	{
         sw=0;
         t=i;
         while (t)
         	{
            if (t%10==c)
            	sw++;
            t*=0.1;
            }
         if (sw>=k)
         	s--;
      	}
   }




void tipareste(void)
	{
   fprintf(g, "%.4f", (float)s/total);
   fcloseall();
   }

int main(void)
{
citeste();
preprocesare();
if (c>0)
	rezolvax();
else
	rezolvaz();
tipareste();
return 0;
}