Cod sursa(job #2785802)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 19 octombrie 2021 16:04:12
Problema Sarpe Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.01 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 3000;
int a[nmax], b[nmax], c[nmax], aux[nmax], d[nmax], ans[nmax];

void Add(int *a, int *b){
    int t = 0;
    a[0] = max(a[0], b[0]);
    for (int i = 1; i <= a[0]; ++i){
        a[i] += (b[i] + t);
        t = a[i] / 10;
        a[i] %= 10;
    }
    if (t){
        a[++a[0]] = t;
    }
}

void Subtract(int *a, int *b){
    int t = 0;
    for (int i = b[0] + 1; i <= a[0]; ++i) b[i] = 0;
    for (int i = 1; i <= a[0]; ++i){
        a[i] = a[i] - b[i] - t;
        if (a[i] < 0){
            a[i] += 10;
            t = 1;
        }
        else{
            t = 0;
        }
    }
    while (!a[a[0]] && a[0] > 1) --a[0];

}

void multiply(int *a, int *b, int *c){
    c[0] = a[0] + b[0];
    for (int i = 1; i <= a[0] + b[0]; ++i){
        c[i] = 0;
    }
    for (int i = 1; i <= a[0]; ++i){
        for (int j = 1; j <= b[0]; ++j){
            c[i + j - 1] += a[i] * b[j];
        }
    }
    int t = 0;
    for (int i = 1; i <= c[0]; ++i){
        c[i] += t;
        t = c[i] / 10;
        c[i] %= 10;
    }
    if (t){
        c[++c[0]] = t;
    }
    while (!c[c[0]] && c[0] > 1) --c[0];
}

void Copy(int *a, int *b){
    a[0] = b[0];
    for (int i = b[0] + 1; i <= a[0]; ++i){
        a[i] = 0;
    }
    for (int i = 1; i <= a[0]; ++i){
        a[i] = b[i];
    }
}

int main(){
    string n;
    fin >> n;
    if (n.size() == 1 && n[0] == '1'){
        fout << 2;
        return 0;
    }
    for (int i = n.size() - 1; i >= 0; --i){
        a[0]++;
        a[a[0]] = n[i] - '0';
        b[0]++;
        b[b[0]] = n[i] - '0';
        d[0]++;
        d[d[0]] = n[i] - '0';
    }
    aux[0] = 1;
    aux[1] = 1;
    Subtract(a, aux);
    aux[1] = 2;
    Subtract(b, aux);
    multiply(a, b, c);
    Add(d, d);
    Add(d, c);
    Add(d, d);
    for (int i = d[0]; i >= 1; --i){
        fout << d[i];
    }
    return 0;
}