Cod sursa(job #2067264)

Utilizator popabogdanPopa Bogdan Ioan popabogdan Data 16 noiembrie 2017 08:44:14
Problema Next Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>

#define ll long long
#define Nmax 1000005

using namespace std;

ifstream fin("next.in");
ofstream fout("next.out");

typedef vector <int> BIG;

BIG A;
char c[Nmax];
ll D;

void read(BIG &A)
{
    fin >> c;
    for(int z = 0; c[z]; z++)
        A.push_back(c[z] - '0');
    reverse(A.begin(), A.end());
}

ll remainder(BIG &A, ll D)
{
    ll t = 0;
    int i;
    for(int i = A.size() - 1; i >= 0; i--)
        t = (t * 10 + A[i]) % D;
    return t;
}

void add(BIG &A, ll X)
{
    ll t = X;
    for(int i = 0; i < A.size() || t; i++, t /= 10)
    {
        if(i == A.size())
            A.push_back(t % 10);
        else
            A[i] = (t += A[i]) % 10;
    }
}

int main()
{
    read(A);
    fin >> D;
    ll toAdd = D - remainder(A, D);
    if(toAdd < D)
        add(A, toAdd);
    for(int i = A.size() - 1; i >= 0; i--)
        fout << A[i];
    fout << "\n";
    return 0;
}