Problem p03082 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int N, X;
int S[220];

template <typename... Args> void debug(Args... args) {
  for (auto arg : {args...})
    cerr << arg << " ";
  cerr << endl;
}

// const int mod = //998244353;
const int mod = 1000000007;
const int FacN = 220; // 使わないなら0

constexpr int power(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res *= a;
    a *= a;
    res %= mod;
    a %= mod;
    b /= 2;
  }
  return res;
}

constexpr int inv(int x) { return power(x, mod - 2); }

const struct Fac {
  int val[FacN];

  constexpr Fac() : val() {
    if (FacN == 0)
      return;
    val[0] = 1;
    for (int i = 1; i < FacN; ++i)
      val[i] = val[i - 1] * i % mod;
  }

  const int &operator[](size_t i) const { return val[i]; }
} fac;

const struct FacInv {
  int val[FacN];

  constexpr FacInv() : val() {
    if (FacN == 0)
      return;
    val[FacN - 1] = 1;
    for (int i = 1; i < FacN; ++i)
      val[FacN - 1] = val[FacN - 1] * i % mod;
    val[FacN - 1] = inv(val[FacN - 1]);
    for (int i = FacN - 1; i >= 1; --i)
      val[i - 1] = val[i] * i % mod;
  }

  const int &operator[](size_t i) const { return val[i]; }
} fac_inv;

int Perm(int n, int r) {
  if (n < r)
    return 0;
  return fac[n] * fac_inv[n - r] % mod;
}

int Comb(int n, int r) { return Perm(n, r) * fac_inv[r] % mod; }
// mod,Factorial書き換え
// 要検証

signed main() {
  cin >> N >> X;
  rep(i, N) { cin >> S[i]; }
  sort(S, S + N, greater<int>());
  map<pii, int> a, b;
  b[pii(0, X)] = 1;
  rep(i, N) {
    for (auto e : b) {
      pii key = e.first;
      int val = e.second;
      a[pii(key.first + 1, key.second)] += val;
      a[pii(key.first + 1, key.second)] %= mod;
      a[pii(0, key.second % S[i])] +=
          val * Perm(N - i - 1 + key.first, key.first);
      a[pii(0, key.second % S[i])] %= mod;
    }
    swap(a, b);
    a.clear();
  }
  int ans = 0;
  for (auto e : b) {
    pii key = e.first;
    int val = e.second;
    if (key.first != 0)
      continue;
    ans += key.second * val;
    ans %= mod;
  }
  (ans += mod) %= mod;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int N, X;
int S[220];

template <typename... Args> void debug(Args... args) {
  for (auto arg : {args...})
    cerr << arg << " ";
  cerr << endl;
}

// const int mod = //998244353;
const int mod = 1000000007;
const int FacN = 220; // 使わないなら0

constexpr int power(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res *= a;
    a *= a;
    res %= mod;
    a %= mod;
    b /= 2;
  }
  return res;
}

constexpr int inv(int x) { return power(x, mod - 2); }

const struct Fac {
  int val[FacN];

  constexpr Fac() : val() {
    if (FacN == 0)
      return;
    val[0] = 1;
    for (int i = 1; i < FacN; ++i)
      val[i] = val[i - 1] * i % mod;
  }

  const int &operator[](size_t i) const { return val[i]; }
} fac;

const struct FacInv {
  int val[FacN];

  constexpr FacInv() : val() {
    if (FacN == 0)
      return;
    val[FacN - 1] = 1;
    for (int i = 1; i < FacN; ++i)
      val[FacN - 1] = val[FacN - 1] * i % mod;
    val[FacN - 1] = inv(val[FacN - 1]);
    for (int i = FacN - 1; i >= 1; --i)
      val[i - 1] = val[i] * i % mod;
  }

  const int &operator[](size_t i) const { return val[i]; }
} fac_inv;

int Perm(int n, int r) {
  if (n < r)
    return 0;
  return fac[n] * fac_inv[n - r] % mod;
}

int Comb(int n, int r) { return Perm(n, r) * fac_inv[r] % mod; }
// mod,Factorial書き換え
// 要検証

signed main() {
  cin >> N >> X;
  rep(i, N) { cin >> S[i]; }
  sort(S, S + N, greater<int>());
  map<pii, int> a, b;
  b[pii(0, X)] = 1;
  rep(i, N) {
    for (auto e : b) {
      pii key = e.first;
      int val = e.second;
      a[pii(1, key.second)] += val * (N - i - 1);
      a[pii(1, key.second)] %= mod;
      a[pii(0, key.second % S[i])] += val;
      a[pii(0, key.second % S[i])] %= mod;
    }
    swap(a, b);
    a.clear();
  }
  int ans = 0;
  for (auto e : b) {
    pii key = e.first;
    int val = e.second;
    if (key.first != 0)
      continue;
    ans += key.second * val;
    ans %= mod;
  }
  (ans += mod) %= mod;
  cout << ans << endl;
  return 0;
}