Cod sursa(job #1051207)

Utilizator Athena99Anghel Anca Athena99 Data 9 decembrie 2013 20:20:41
Problema Next Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <cstring>
#include <fstream>

using namespace std;

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

typedef long long i64;

const int base= 10;
const int nmax= 1000000;

i64 d;
char s[nmax+1];
int v[nmax+1];

int main(  ) {
    fin>>s>>d;
    int n= strlen(s);
    for ( int i= 1; i<=n; ++i ) {
        v[n-i+1]= (int)s[i-1]-'0';
    }

    i64 t= 0;
    for ( int i= n; i>0; --i ) {
        t= (t*base+v[i])%d;
    }
    t= (d-t)%d;

    for ( int i= 1; i<=n || t>0; ++i ) {
        if ( i>n ) {
            ++n;
        }
        v[i]+= t; 
        t= v[i]/base;
        v[i]%= base;
    }

    for ( int i= n; i>0; --i ) {
        fout<<v[i];
    }
    fout<<"\n";

    return 0;
}