Cod sursa(job #53112)

Utilizator Mishu91Andrei Misarca Mishu91 Data 21 aprilie 2007 00:03:21
Problema Next Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.13 kb
#include<cstdio>

int a[1100000];

void div(int A[],unsigned long long B)  
{  
       long  i, t = 0;  
         for (i = A[0]; i > 0; i--, t %= B)  
                  A[i] = (t = t * 10 + A[i]) / B;  
          for (; A[0] > 1 && !A[A[0]]; A[0]--);  
   }  
void mul(int c[],unsigned long long d)  
{  
  int i, t = 0;  
  for (i = 1; i <= c[0] || t; i++, t /= 10)  
    c[i] = (t += c[i] * d) % 10;  
  c[0] = i - 1;  
} 
int mod(int A[],unsigned long long B)  
{  
  int i, t = 0;  
  for (i = A[0]; i > 0; i--)  
    t = (t * 10 + A[i]) % B;  
  return t;  
}  
int main()
{    
  freopen("next.in","r",stdin);
  freopen("next.out","w",stdout);
  char ch;
  int x[1100010];
  x[0]=0;
  int i;
  while(1)
  {
     ch=fgetc(stdin);
     if(ch>'9'||ch<'0') break;
     x[++x[0]]=ch-'0';
  }
  
  a[0]=x[0];
  for(i=1;i<=x[0];i++)
    a[i]=x[x[0]+1-i];
  unsigned long long d;
  scanf("%lld",&d);
  
  if(mod(a,d))
  {
    div(a,d);
  
    i=1;
    while(a[i]==9) 
      a[i++]=0;
    a[i]++;
    if(i>a[0]) a[0]=i;
    mul(a,d);
  }
  for(i=a[0];i>=1;i--) printf("%d",a[i]);
  return 0;
}