Cod sursa(job #38342)

Utilizator stef2nStefan Istrate stef2n Data 25 martie 2007 18:12:21
Problema Next Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <stdio.h>
#include <string.h>

#define infile "next.in"
#define outfile "next.out"
#define LMAX 1000002
#define BAZA 10000
struct NUMAR{int n,x[250100];};

FILE *fin,*fout;
char sir[LMAX];
long long D;
NUMAR N;
long long surplus;

void read_data()
  {
   fin=fopen(infile,"r");
   fgets(sir,LMAX,fin);
   fscanf(fin,"%Ld\n",&D);
   fclose(fin);
  }

void init()
  {
   int i,aux,cnt,p10;
   N.n=0;
   i=strlen(sir)-2;
   while(i>=0)
        {
         aux=0;
         p10=1;
         for(cnt=0;(cnt<4)&&(i>=0);cnt++)
            {
             aux=aux+(sir[i--]-'0')*p10;
             p10*=10;
            }
         N.x[N.n++]=aux;
        }
  }

void write_sol(NUMAR a)
  {
   fout=fopen(outfile,"w");
   fprintf(fout,"%d",a.x[a.n-1]);
   a.n-=2;
   while(a.n>=0)
        {
         for(int p10=BAZA/10;p10>=1;p10/=10)
            fprintf(fout,"%d",(a.x[a.n]%(p10*10))/p10);
         a.n--;
        }
   fclose(fout);
  }

NUMAR suma(NUMAR a, long long t)
  {
   int i=0,aux;
   while(t>0 && i<a.n)
        {
         aux=t%BAZA;
         a.x[i]=a.x[i]+aux;
         t/=BAZA;
         t=t+a.x[i]/BAZA;
         a.x[i]%=BAZA;
         i++;
        }
   while(t)
        {
         a.x[i]=t%BAZA;
         t/=BAZA;
         i++;
         a.n++;
        }
   return a;
  }

void solve()
  {
   long long rest=0;
   for(int i=N.n-1;i>=0;i--)
      {
       rest=(rest*BAZA+N.x[i])%D;
      }
   if(rest!=0)
     surplus=D-rest;
  }


int main()
{
read_data();
init();
solve();
write_sol(suma(N,surplus));
return 0;
}