Mai intai trebuie sa te autentifici.

Cod sursa(job #361470)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 5 noiembrie 2009 12:22:23
Problema Next Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <stdio.h>
#define Nmax 1000100
#define LL long long

int a[Nmax],i,ok,v[100],aux2;
char c;
LL D,t,aux;

int main(){
	freopen("next.in","r",stdin);
   freopen("next.out","w",stdout);
   for(ok=1; ok;  ){
   	  scanf("%c",&c);
      if( c >='0' && c<='9' ) a[++a[0]]=c-'0';
      else ok=0;
   }
   scanf("%lld",&D);

 	// fac rest
   for(i=1,t=0;i<=a[0];++i)
   	t=( t*10 + a[i] ) % D;

   if(t == 0)
   	for(i=1;i<=a[0];++i) printf("%d",a[i]);
   else{
      for(i=1;i<=a[0]/2;++i){
      	aux2=a[i]; a[i]=a[a[0]-i+1];
         a[a[0]-i+1]=aux2;
      }
   	  aux = D-t;
      while( aux > 0){ v[++v[0]]=aux % 10; aux = aux/10; }

     for(i=1,t=0; i<=a[0] || i<=v[0] || t; i++, t/=10)
     	a[i] = ( t+= a[i]+v[i] ) % 10;
     a[0]=i-1;
     // adun(a,v);

      for(i=a[0]; i>=1; --i) printf("%d",a[i]);
   }

   printf("\n");
   fclose(stdin); fclose(stdout);
   return 0;
}