Cod sursa(job #3367031)

Utilizator MateiDiaconuDiaconu Matei Stefan MateiDiaconu Data 5 octombrie 2026 19:23:48
Problema Next Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <iostream>

using namespace std;

#define MAXC 1000000

FILE *fin, *fout;

struct BigInt{
    char v[MAXC];
    int nrcif;
};

BigInt n;

// a = a + b
void add(BigInt& a, long long b){
    int i = 0;

    while(b > 0){
        b += a.v[i];
        a.v[i] = b % 10;
        b /= 10;
        i++;
    }

    if(i > a.nrcif) {
        a.nrcif = i;
    }
}

long long divide(BigInt& a, long long d) {
    int i;
    long long r = 0;

    for(i = a.nrcif - 1; i >= 0; i--){
        r = r * 10 + a.v[i];
        r = r % d;
    }

    return r;
}

void print(BigInt& a) {
    for(int i = a.nrcif - 1; i >= 0; i--) {
        fprintf(fout, "%d", a.v[i]);
    }
}

int main() {
    fin = fopen("next.in", "r");
    fout = fopen("next.out", "w");

    long long d;
    int i;
    char ch;

    while((ch = fgetc(fin)) != '\n') {
        n.v[n.nrcif++] = ch - '0';
    }

    for(i = 0; i < n.nrcif / 2; i++) {
        swap(n.v[i], n.v[n.nrcif - i - 1]);
    }

    fscanf(fin, "%lld", &d);

    long long r = divide(n, d);

    if(r > 0) {
        add(n, d - r);
    }

    print(n);

    fclose(fin);
    fclose(fout);
    return 0;
}