Cod sursa(job #471612)

Utilizator titusuTitus C titusu Data 19 iulie 2010 19:51:09
Problema Next Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
//#include <cstdio>
#include <fstream>
#include <cstring>
using namespace std;
char v[1000005];
int a[1000005],n;

int main(){
  long long D;
  ifstream fin("next.in");
  ofstream fout("next.out");
  fin.getline(v,sizeof(v));
  if(v[strlen(v)-1]=='\n')
    v[strlen(v)-1] = 0;
  fin>>D;
  n = 0;
  for(int i=strlen(v)-1;i>=0;--i)
    a[++n]=v[i]-'0';
  a[0]=n;
  
  long long t=0;
  for(int i=a[0];i;--i)
    t = (10*t+a[i])%D;
  if(t==0)
    D = 0;
  else
    D -= t;
  t= 0 ;
  int i = 0;
  while (D){
    if(i+1>a[0])
      a[0]++;
    i++;
    int tmp = a[i]+D%10+t;
    a[i] = tmp % 10;
    t = tmp / 10;
    D/=10;
  }
  
  while(t){
    if(i+1>a[0])
      a[0]++;
    a[++i] += t%10, t/=10;
  }
  for(int i=a[0];i;--i)
    fout<<a[i];
  fout<<endl;
  return 0;
}