Cod sursa(job #761460)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 26 iunie 2012 00:33:51
Problema Next Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <fstream>
#include <algorithm>
#include <string.h>

#define MAX 1000050

using namespace std;

int nr, numar[MAX], NR[100], MOD[100];
char sir[MAX];

void citire()
{
    ifstream in("next.in");
    in.getline(sir, MAX);
    int lgt = strlen(sir);
    for(int i = lgt; i; i--)
        numar[i] = sir[lgt - i] - '0';
    numar[0] = lgt;
    in>>nr;
    in.close();
}

long long mod(int A[], long long B)
{
      long long t = 0, i;
      for (i = A[0]; i > 0; i--)
              t = (t * 10 + A[i]) % B;
      return t;
}

void add(int A[], int B[])
{
      long long i, t = 0;
      for (i=1; i<=A[0] || i<=B[0] || t; i++, t/=10)
              A[i] = (t += A[i] + B[i]) % 10;
      A[0] = i - 1;
}

void sub(int A[], int B[])
{
      long long i, t = 0;
      for (i = 1; i <= A[0]; i++) {
              A[i] -= ((i <= B[0]) ? B[i] : 0) + t;
              A[i] += (t = A[i] < 0) * 10;
      }
      for (; A[0] > 1 && !A[A[0]]; A[0]--);
}

void afisare()
{
    ofstream out("next.out");
    for(int i = numar[0]; i; i--)
        out<<numar[i];
    out.close();
}

void toBig(int A[], long long x)
{
    int lgt = 0;
    while(x)
    {
        A[++lgt] = x % 10;
        x /= 10;
    }
    A[0] = lgt;
}
int main()
{
    citire();
    ofstream out("next.out");
    if(mod(numar, nr))
    {
        toBig(NR, nr);
        toBig(MOD, mod(numar, nr));
        add(numar, NR);
        sub(numar, MOD);
    }
    afisare();
    return 0;
}