Cod sursa(job #652133)

Utilizator daniel.dumitranDaniel Dumitran daniel.dumitran Data 23 decembrie 2011 04:34:10
Problema Numere 2 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.16 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <stdint.h>
#include <string>

class BigInt {
public:
  explicit BigInt(uint64_t x = 0) {
    m_len = 10;
    memset(m_cif, 0, sizeof(m_cif));
    for (uint32_t i = 0; x; ++i) {
      m_cif[i] = x % sm_base;
      x /= sm_base;
    }
    fix();
  }

  explicit BigInt(const std::string& number) {
    const char* digits = number.c_str();
    m_len = 0;
    memset(m_cif, 0, sizeof(m_cif));    

    char buff[5];

    int poz = strlen(digits) - 4;
    while (poz >= 0) {
      memset(buff, 0, sizeof(buff));
      strncpy(buff, digits + poz, 4);
      m_cif[m_len++] = atoi(buff);
      poz -= 4;
    }
    
    memset(buff, 0, sizeof(buff));
    strncpy(buff, digits, 4 + poz);
    m_cif[m_len++] = atoi(buff);
    fix();
  }

  BigInt& operator*=(uint32_t scalar) {
    for (int i = m_len - 1; i >= 0; --i) {      
      uint64_t temp = ((uint64_t)m_cif[i]) * ((uint64_t)scalar);
      m_cif[i + 2] += (temp / sm_base) / sm_base;
      m_cif[i + 1] += (temp / sm_base) % sm_base;
      m_cif[i] = temp % sm_base;
    }
    m_len += 2;
    fix();
    return *this;
  }

  BigInt& operator+=(const BigInt& other) {
    for (uint32_t i = 0; i < other.m_len; ++i)
      m_cif[i] += other.m_cif[i];
    m_len = std::max(m_len, other.m_len);
    fix();
    return *this;
  }

  BigInt& operator+=(long long other) {
    m_cif[0] += other;
    fix();
    return *this;
  }

  BigInt operator*(const BigInt& other) {
    BigInt result(0);
    for (uint32_t i = 0; i < m_len; ++i)
      for (uint32_t j = 0; j < other.m_len; ++j) {
	result.m_cif[i + j] += m_cif[i] * other.m_cif[j];
	result.m_cif[i + j + 1] += result.m_cif[i + j] / sm_base;
	result.m_cif[i + j] %= sm_base;
      }
    result.m_len = m_len + other.m_len;
    result.fix();
    return result;
  }

  BigInt& operator/=(unsigned long div) {
    long long num = 0;
    for (int i = m_len - 1; i >= 0; --i) {
      num *= sm_base;
      num += m_cif[i];
      m_cif[i] = num / div;
      num %= div;
    }

    return *this;
  }

  bool operator<(const BigInt& other) { return compare(other) < 0; }
  bool operator<=(const BigInt& other) { return compare(other) <= 0; }

  bool operator>(const BigInt& other) { return compare(other) > 0; }
  bool operator>=(const BigInt& other) { return compare(other) >= 0; }

  bool operator==(const BigInt& other) { return compare(other) == 0; }

  BigInt operator+(const BigInt& other) {
    BigInt result(0);
    result.m_len = m_len;
    memcpy(&result.m_cif, &m_cif, sizeof(m_cif));

    result += other;
    return result;
  }

  BigInt operator+(long long other) {
    BigInt result(0);
    result.m_len = m_len;
    memcpy(&result.m_cif, &m_cif, sizeof(m_cif));

    result += other;
    return result;
  }

  void print(std::string& output) {
    fix();
    if (!m_len) { output = "0"; return; }
    
    output.clear();
    add_digit(m_cif[m_len - 1], false, output);
    for (int i = m_len - 2; i >= 0; --i)
      add_digit(m_cif[i], true, output);
  }

  void raw_print() {
    fix();
    
    printf("%lu :", (unsigned long)m_len);
    for (int i = m_len - 1; i >= 0; --i)
      printf(" %lu", (unsigned long)m_cif[i]);
    printf("\n");
  }
  
private:
  void fix() {
    for (int i = 0; i < (signed)m_len; ++i) {
      if (m_cif[i] >= sm_base) {
	m_cif[i + 1] += m_cif[i] / sm_base;
	m_cif[i] %= sm_base;
	if (i + 1 == (signed)m_len) ++m_len;
      }

      if (m_cif[i] < 0) {
	int borrow = (-m_cif[i] + sm_base - 1) / sm_base;
	m_cif[i] += borrow * sm_base;
	m_cif[i + 1] -= borrow;
      }
    }
    while (m_len && m_cif[m_len - 1] == 0) --m_len;
  }

  void add_digit(uint32_t digit, bool force_len, std::string& str) {
    char buffer[128];
    sprintf(buffer, force_len ? "%4.4lu" : "%lu", (unsigned long)digit);
    str += buffer;
  }

  int compare(const BigInt& other) {
    if (m_len != other.m_len)
      return m_len - other.m_len;

    for (int i = m_len - 1; i >= 0; --i)
      if (m_cif[i] != other.m_cif[i])
	return m_cif[i] - other.m_cif[i];

    return 0;
  }

  static const uint32_t sm_base = 10000;
  uint32_t m_cif[8192], m_len;
};


#define FISIN   "numere2.in"
#define FISOUT  "numere2.out"

#define MAXLEN  100
#define MAXPOW  4 * MAXLEN

int bad_prime[MAXPOW];

BigInt fastExp(BigInt num, int pow) {
  if (pow == 0) { return BigInt(1); }
  if (pow == 1) { return num; }

  BigInt sqrt = fastExp(num, pow / 2);
  BigInt temp = sqrt * sqrt;
  return (pow & 1) ? temp * num : temp;
}

int main() {
  FILE *fin = fopen(FISIN, "rt");
  FILE *fout = fopen(FISOUT, "wt");

  char buffer[128];
  fscanf(fin, "%s", buffer);
  BigInt target(buffer);
  int power = 1;

  for (int i = 2; i < MAXPOW; ++i) {
    if (bad_prime[i]) continue;
    for (int j = i * 2; j < MAXPOW; j += i)
      bad_prime[j] = 1;

    BigInt st(1);
    BigInt en(target);
    
    while (st <= en) {
      BigInt mid = st + en;
      mid /= 2;

      BigInt pow = fastExp(mid, i);

      if (pow == target) {
	power *= i;
	target = mid;
	--i;
	break;
      }

      if (pow < target) {
	st = mid + 1;
      } else {
	en = mid + (-1);
      }
    }
  }

  std::string output; target.print(output);
  
  fprintf(fout, "%s\n%d\n", output.c_str(), power);

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