Cod sursa(job #1425289)

Utilizator BLz0rDospra Cristian BLz0r Data 27 aprilie 2015 11:17:59
Problema Next Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <fstream>
#include <vector>
#include <string>
using namespace std;

#define Nmax 10000005

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

int A[Nmax], B[Nmax];
string S;

long long Modulo ( int A[], int Mod ){
    long long rez = 0;

    for ( int i = A[0]; i >= 1; --i )
        rez = ( rez * 10 + A[i] ) % Mod;

    return rez;
}

void Adun ( int A[], int B[] ){

    int aux = 0, i;

    for ( i = 1; i <= A[0] || i <= B[0] || aux; ++i, aux /= 10 )
        A[i] = ( aux += A[i] + B[i] ) %10;
    A[0] = i-1;
}


int main(){
    vector < int > :: iterator it;
    long long D;

    fin >> S >> D;

    for ( int i = S.size() - 1; i >= 0; --i ){
        int cif = S[i] - '0';
        if ( cif >= 0 && cif <= 9 )
            A[++A[0]] = cif;
    }

    S.clear();

    long long rest = Modulo ( A, D );

    if ( rest > 0 ){
        D -= rest;

        while ( D ){
            B[++B[0]] = D%10;
            D /= 10;
        }

        Adun ( A, B );
    }

    for ( int i = A[0]; i >= 1; --i )
        fout << A[i];

    return 0;
}