Cod sursa(job #1856779)

Utilizator TincaMateiTinca Matei TincaMatei Data 25 ianuarie 2017 14:02:22
Problema Patrate2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.77 kb
#include <cstdio>

const int MAX_CIFRE = 500;
const int BASE = 10000000;

class Huge {
public:
  int n;
  int cf[MAX_CIFRE];
  Huge() {
    int i;
    for(i = 0; i < MAX_CIFRE; i++)
      cf[i] = 0;
    n = 1;
  }
  Huge(int x) {
    int i = 0;
    while(x > 0) {
      cf[i] = x % BASE;
      x = x / BASE;
      i++;
    }
    n = i;
    while(i < MAX_CIFRE) {
      cf[i] = 0;
      i++;
    }
  }
  Huge operator+(int x) {
    Huge rez(0);
    int tr, i;
    i = 0;
    tr = 0;
    while(i < this->n || x > 0 || tr > 0) {
      tr = (tr + this->cf[i] + x % BASE);
      rez.cf[i] = tr % BASE;
      x = x / BASE;
      tr = tr / BASE;
      i++;
    }
    rez.n = i;
    return rez;
  }
  Huge operator+(Huge x) {
    Huge rez(0);
    int tr, i;
    i = 0;
    tr = 0;
    while(i < this->n || i < x.n || tr > 0) {
      tr = (tr + this->cf[i] + x.cf[i]);
      rez.cf[i] = tr % BASE;
      tr = tr / BASE;
      i++;
    }
    rez.n = i;
    return rez;
  }
  Huge operator*(int x) {
    Huge rez(0);
    int tr, i = 0;
    tr = 0;
    while(i < this->n || tr > 0) {
      tr = (tr + this->cf[i] * x);
      rez.cf[i] = tr % BASE;
      tr = tr / BASE;
      i++;
    }
    rez.n = i;
    return rez;
  }
};

void putHuge(FILE *fout, Huge a) {
  int i, p10;
  i = MAX_CIFRE - 1;
  while(i > 0 && a.cf[i] == 0)
    i--;
  fprintf(fout, "%d", a.cf[i]);
  i--;
  while(i >= 0) {
    p10 = BASE / 10;
    while(p10 > 0) {
      fprintf(fout, "%d", a.cf[i] / p10 % 10);
      p10 = p10 / 10;
    }
    i--;
  }
}
int main() {
  int n;
  FILE *fin = fopen("patrate2.in", "r");
  fscanf(fin, "%d", &n);
  fclose(fin);

  Huge x(1);
  for(int i = 1; i <= n; ++i)
    x = x * i;
  for(int i = 0; i < n * n; ++i)
    x = x * 2;

  FILE *fout = fopen("patrate2.out", "w");
  putHuge(fout, x);
  fclose(fout);
  return 0;
}