Problem p02618 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <queue>
#include <random>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int(i) = 1; (i) <= (n); (i)++)

using namespace std;

class Xorshift {
public:
  using result_type = uint_fast32_t;

  /**
   * @brief returns minimum value
   *
   * @return minimum value
   */
  inline static constexpr unsigned min() { return 0u; }

  /**
   * @brief returns maximum value
   *
   * @return maximum value
   */
  inline static constexpr unsigned max() { return UINT_MAX; }

  /**
   * @brief generate random number
   *
   * @return random number
   */
  inline unsigned operator()() {
    unsigned t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  /**
   * @brief Xorshift constructor
   */
  Xorshift() {
    std::random_device rd;
    w = rd();
  }

  /**
   * @brief Xorshift constructor with seed
   *
   * @param s seed
   */
  Xorshift(unsigned s) { w = s; }

private:
  unsigned x = 123456789u, y = 362436069u, z = 521288629u, w;
};

constexpr int TYPE = 26;
int D;
vector<int> c;
vector<int> s;

inline int _(int d, int i) { return TYPE * d + i; }

inline int score(const vector<int> &t) {
  vector<int> last(TYPE, 0);
  int ret = 0;
  rep(d, D) {
    ret += s[_(d, t[d] - 1)];
    last[t[d] - 1] = d + 1;
    rep(i, TYPE) { ret -= c[i] * (d + 1 - last[i]); }
  }
  return ret;
}

inline void swap(vector<int> &t, double beta, Xorshift &rand_gen) {
  static auto uid = std::uniform_int_distribution<>(0, D - 1);
  static auto uid2 = std::uniform_int_distribution<>(1, TYPE);
  static auto urd = std::uniform_real_distribution<>(0, 1.0);
  int pos = uid(rand_gen);
  int new_val = uid2(rand_gen);
  int old_val = t[pos];
  double Ebef = -score(t);
  t[pos] = new_val;
  double Eaft = -score(t);
  t[pos] = old_val;
  double dE = Eaft - Ebef;
  if (dE < 0 || std::exp(-beta * dE) > urd(rand_gen)) {
    t[pos] = new_val;
  }
}

template <typename T = double> class Logspace {
private:
  T curValue, base, step;

public:
  Logspace(T first, T last, int num, T base = 10.0)
      : curValue(first), base(base) {
    step = (last - first) / (num - 1);
  }

  T operator()() {
    T retval = pow(base, curValue);
    curValue += step;
    return retval;
  }
};

int main(int argc, char const *argv[]) {
  c.resize(TYPE);
  cin >> D;
  rep(i, TYPE) { cin >> c[i]; }
  s.resize(D * TYPE);
  rep(d, D) {
    rep(i, TYPE) { cin >> s[_(d, i)]; }
  }

  Xorshift rand_gen(0x1234);

  vector<int> t;
  static auto uid2 = std::uniform_int_distribution<>(1, TYPE);
  for (int d = 0; d < D; d++) {
    t.push_back(uid2(rand_gen));
  }

  vector<double> beta_array;
  int num = 1000;
  std::generate_n(std::back_inserter(beta_array), num,
                  Logspace<>(std::log10(0.1), std::log10(50), num));
  for (double beta : beta_array) {
    swap(t, beta, rand_gen);
  }
  for (int elem : t) {
    cout << elem << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <queue>
#include <random>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int(i) = 1; (i) <= (n); (i)++)

using namespace std;

class Xorshift {
public:
  using result_type = uint_fast32_t;

  /**
   * @brief returns minimum value
   *
   * @return minimum value
   */
  inline static constexpr unsigned min() { return 0u; }

  /**
   * @brief returns maximum value
   *
   * @return maximum value
   */
  inline static constexpr unsigned max() { return UINT_MAX; }

  /**
   * @brief generate random number
   *
   * @return random number
   */
  inline unsigned operator()() {
    unsigned t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
  }

  /**
   * @brief Xorshift constructor
   */
  Xorshift() {
    std::random_device rd;
    w = rd();
  }

  /**
   * @brief Xorshift constructor with seed
   *
   * @param s seed
   */
  Xorshift(unsigned s) { w = s; }

private:
  unsigned x = 123456789u, y = 362436069u, z = 521288629u, w;
};

constexpr int64_t TYPE = 26;
int64_t D;
vector<int64_t> c(TYPE);
vector<int64_t> s(D *TYPE);

inline int64_t _(int64_t d, int64_t i) { return TYPE * d + i; }

int64_t score(const vector<int64_t> &t) {
  vector<int64_t> last(TYPE, 0);
  int64_t ret = 0;
  rep(d, D) {
    ret += s[_(d, t[d] - 1)];
    last[t[d] - 1] = d + 1;
    cout << endl;
    rep(i, TYPE) { ret -= c[i] * (d + 1 - last[i]); }
  }
  return ret;
}

inline void swap(vector<int64_t> &t, double beta, Xorshift &rand_gen) {
  static auto uid = std::uniform_int_distribution<>(0, 25);
  static auto urd = std::uniform_real_distribution<>(0, 1.0);
  int t1 = uid(rand_gen);
  int t2 = uid(rand_gen);
  double Ebef = -score(t);
  std::swap(t[t1], t[t2]);
  double Eaft = -score(t);
  std::swap(t[t1], t[t2]);
  double dE = Eaft - Ebef;
  if (dE < 0 || std::exp(-beta * dE) > urd(rand_gen)) {
    std::swap(t[t1], t[t2]);
  }
}

template <typename T = double> class Logspace {
private:
  T curValue, base, step;

public:
  Logspace(T first, T last, int num, T base = 10.0)
      : curValue(first), base(base) {
    step = (last - first) / (num - 1);
  }

  T operator()() {
    T retval = pow(base, curValue);
    curValue += step;
    return retval;
  }
};

int main(int argc, char const *argv[]) {
  c.resize(TYPE);
  cin >> D;
  rep(i, TYPE) { cin >> c[i]; }
  s.resize(D * TYPE);
  rep(d, D) {
    rep(i, TYPE) { cin >> s[_(d, i)]; }
  }

  // cout << score({1,17,13,14,15}) << endl;

  vector<int64_t> t = {1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13,
                       14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26};

  Xorshift rand_gen(0x1234);

  vector<double> beta_array;
  int num = 40;
  std::generate_n(std::back_inserter(beta_array), num,
                  Logspace<>(std::log10(0.01), std::log10(50), num));
  for (double beta : beta_array) {
    swap(t, beta, rand_gen);
  }
  for (int64_t elem : t) {
    cout << elem << endl;
  }

  return 0;
}