Cod sursa(job #988110)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 22 august 2013 00:14:06
Problema Next Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

#define Nmax 1500000

typedef unsigned BigInteger[Nmax];

BigInteger N;
string s;
long long D;

void read()
{
    ifstream f("next.in");

    f >> s >> D;

    f.close();
}

void init()
{
    for ( int i = s.length() - 1; i >= 0; i-- )
            N[ ++N[0] ] = s[i] - 48;
}

long long modulo( BigInteger a, long long divi )
{
    long long R = 0;

    for ( long long i = a[0]; i; i-- )
    {
        R = R * 10 + a[i];
        R %= divi;
    }

    return R;
}

void inmult( BigInteger &a, long long x )
{
    long long T = 0;

    for ( long long i = 1; i <= a[0]; ++i )
    {
        a[i] = ( a[i] * x + T );
        T = a[i] / 10;
        a[i] %= 10;
    }

    while ( T )
            a[ ++a[0] ] = T % 10,
            T /= 10;
}

void aduna( BigInteger &a, long long x )
{
    long long T = x;

    for ( long long i = 1; i <= a[0]; ++i )
    {
        a[i] = ( a[i] + T );
        T = a[i] / 10;
        a[i] %= 10;
    }

    if ( T )
            a[ ++a[0] ] = T;
}

void afis( BigInteger a )
{
    ofstream g("next.out");

    for ( long long i = a[0]; i >= 1; i-- )
            g << a[i] ;

    g << "\n";

    g.close();
}

int main()
{
    read();
    init();

    long long R = modulo( N, D );

    if ( R )
        aduna( N, D - R );

    afis( N );

    return 0;
}