Cod sursa(job #2108530)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 18 ianuarie 2018 14:54:12
Problema Next Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("next.in");
ofstream g("next.out");

short N[1000010], rez[1000010], Ncop[1000010], Di[1000010], one[1000010] = {0, 1};
long long D;
char str[1000010];
long long divide(short a[], long long nr)
{
    long long rest = 0;
    for (int i = a[0]; i >= 1; i--) {
        rest = 10 * rest + a[i];
        a[i] = rest / nr;
        rest %= nr;
    }
    while (a[0] > 1 && !a[a[0]]) a[0]--;
    return rest;
}
void add(short a[], short b[])
{
    int transport = 0;
    for (int i = 1; i <= a[0]; i++) {
        a[i] += b[i] + transport;
        transport = a[i] / 10;
        a[i] %= 10;
    }
    if (transport) a[++a[0]] = transport;
}

void multiply(short a[], short b[], short rez[])
{
    int transport = 0;
    rez[0] = a[0] + b[0] - 1;
    for (int i = 1; i <= a[0]; i++)
        for (int j = 1; j <= b[0]; j++)
            rez[i + j - 1] += a[i] * b[j];
    for (int i = 1; i <= rez[0]; i++) {
        rez[i] += transport;
        transport = rez[i] / 10;
        rez[i] %= 10;
    }
    if (transport) rez[++rez[0]] = transport;
}
int main()
{
    f >> str >> D;
    N[0] = strlen(str);
    for(int i = 1; i <= N[0]; i++)
        N[i] = str[N[0] - i] - '0';
    int rest = divide(N, D);
    if(rest) add(N, one);
    while(D) {
        Di[++Di[0]] = D % 10;
        D /= 10;
    }
    multiply(N, Di, rez);
    for(int i = rez[0]; i >= 1; i--)
        g << rez[i];
    g << "\n";
    return 0;
}