Cod sursa(job #1787296)

Utilizator Y.MalmsteenB.P.M. Y.Malmsteen Data 24 octombrie 2016 13:48:13
Problema Next Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <iostream>
#include <fstream>

using namespace std;

const int MAXN = 1000000;
char N[MAXN + 1];
long long D, rest;
int lN;

ifstream f("next.in");
ofstream g("next.out");

void afisare(char A[], int lA)
{
    for(int i = lA; i >= 1; i--)
        g << int (A[i]);
}

void citire()
{
    char c;
    lN = 0;
    f.get(c);
    while(c != '\n')
    {
        N[++lN] = c-'0';
        f.get(c);
    }
    for(int i = lN, j = 1; i > j; i--, j++)
        swap(N[i], N[j]);
    f >> D;
}

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

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

int main()
{
    citire();
    rest = mod(N, lN, D);
    if(rest > 0)
        add(N, lN, D - rest);
    afisare(N, lN);
    f.close();
    g.close();
    return 0;
}