Cod sursa(job #2013451)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 21 august 2017 14:56:33
Problema Next Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("next.in");
ofstream out("next.out");
const int maxn = 1001000;
short int A[maxn];
short int B[maxn];
int szb = 0;
int sz = 0;
void add(short int A[], short int B[])
{
    int tr = 0;
    if(szb > sz)
    {
        for(int i = sz + 1; i <= szb; i++)
            A[i]=0;
        sz = szb;
    }
    else
        for(int i = szb + 1; i <= sz; i++)
            B[i] = 0;
    for(int i = 1; i <= sz; i++)
    {
        A[i] = A[i] + B[i] + tr;
        tr = A[i] / 10;
        A[i] = A[i] % 10;
    }
    if(tr > 0)
        A[++sz] = tr;
}

long long rest(short int A[], long long d)
{
    long long r = 0;
    for(int i = sz; i >= 1; i--)
        r = (r * 10 + A[i]) % d;
    if(r == 0)
        return 0;
    return (d - r);
}

void convert(long long x)
{
    if(x == 0)
    {
        szb = 1;
        return;
    }
    while(x > 0)
    {
        B[++szb] = x % 10;
        x = x / 10;
    }
    return;
}

int main()
{
    string s;
    in >> s;
    long long d;
    in >> d;
    for(int i = s.size() - 1; i >= 0; i--)
        A[++sz] = s[i] - '0';
    convert(rest(A, d));
    //for(int i = szb; i >= 1; i--)
        //cerr << B[i];
    //cerr << "\n";
    add(A, B);
    for(int i = sz; i >= 1; i--)
        out << A[i];
    out << "\n";
    return 0;
}