Cod sursa(job #3367472)

Utilizator ChillingManMihai-Tudor ChillingMan Data 8 octombrie 2026 01:02:18
Problema Next Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.85 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 1000000;
char a[NMAX + 1];
char rasp[NMAX / 16];

void invers(char rasp[], int cnt){
    char st, dr, aux;
    st = 0;
    dr = cnt - 1;
    while (st < dr){
      aux = rasp[st];
      rasp[st] = rasp[dr];
      rasp[dr] = aux;
      st++;
      dr--;
    }
}

void inmultire(char rasp[], int &cnt, int b){
    int x, i;
    x = 0;
    i = 0;
   // cout << rasp[0];
    while (i < cnt || x > 0){
      if (i >= cnt){
        rasp[i] = 0;
      }
      x = x + rasp[i] * b;
      rasp[i++] = x % 10;
      x /= 10;
    }
    if (cnt < i){
      cnt = i;
    }
    //cout << b;
}

int main()
{
    int cifa, cifb, bb, i, sumcifb, cnt;
    long long b, x;
    char aux;

    cifa = 0;
    while (fin.get(aux)){
      if (aux == ' ' || aux == '\n'){
        break;
      }
      if (aux >= '0' && aux<= '9')
      a[cifa++] = aux - '0';
      //fout << int(a[cifa - 1]) << " ";
    }

    fin >> b;
    bb = b;
    cifb = 0;
    while (bb > 0){
      cifb++;
      bb /= 10;
    }

    sumcifb = cifb;
    cnt = 0;
    x = 0;
    while (sumcifb <= cifa){
      for (i = sumcifb - cifb ; i < sumcifb ; i++){
        x = x * 10 + a[i];
      }
      rasp[cnt++] = x / b;
      //fout << int(rasp[cnt - 1]) << " ";
      x = x % b;
      sumcifb = sumcifb + cifb;
    }
    for (i = sumcifb - cifb ; i < cifa ; i++){
      x = x * 10 + a[i];
    }
    cnt--;
    if (x > b){
      rasp[cnt] = x / b + 1;
      //fout << int(rasp[cnt]);
    }
    else {
      rasp[cnt]++;
    }
    cnt++;

    //cout << int(rasp[0]);// << " " << cnt;
    invers(rasp, cnt);
    inmultire(rasp, cnt, b);

    for (i = cnt - 1 ; i >= 0  ; i--){
      fout << int(rasp[i]);
    }
}