Problem p03082 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define Rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define Repe(i, a, b) for (register int i = (a); i >= (b); --i)
#define rep(i, a, b) for (register int i = (a); i < (b); ++i)
#define pb push_back
#define mp make_pair
#define mx(a, b) (a > b ? a : b)
#define mn(a, b) (a < b ? a : b)
typedef unsigned long long uint64;
typedef unsigned int uint32;
typedef long long ll;
using namespace std;

namespace IO {
const uint32 Buffsize = 1 << 15, Output = 1 << 24;
static char Ch[Buffsize], *S = Ch, *T = Ch;
inline char getc() {
  return ((S == T) && (T = (S = Ch) + fread(Ch, 1, Buffsize, stdin), S == T)
              ? 0
              : *S++);
}
static char Out[Output], *nowps = Out;

inline void flush() {
  fwrite(Out, 1, nowps - Out, stdout);
  nowps = Out;
}

template <typename T> inline void read(T &x) {
  x = 0;
  static char ch;
  T f = 1;
  for (ch = getc(); !isdigit(ch); ch = getc())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getc())
    x = x * 10 + (ch ^ 48);
  x *= f;
}

template <typename T> inline void write(T x, char ch = '\n') {
  if (!x)
    *nowps++ = '0';
  if (x < 0)
    *nowps++ = '-', x = -x;
  static uint32 sta[111], tp;
  for (tp = 0; x; x /= 10)
    sta[++tp] = x % 10;
  for (; tp; *nowps++ = sta[tp--] ^ 48)
    ;
  *nowps++ = ch;
  if (nowps - Out >= 1 << 23)
    flush();
}
} // namespace IO
using namespace IO;

void file() {
#ifndef ONLINE_JUDGE
  FILE *DSD = freopen("water.in", "r", stdin);
  FILE *CSC = freopen("water.out", "w", stdout);
#endif
}

inline void Chkmin(int &u, int v) { u > v ? u = v : 0; }

inline void Chkmax(int &u, int v) { u < v ? u = v : 0; }

inline void Chkmax(ll &u, ll v) { u < v ? u = v : 0; }

inline void Chkmin(ll &u, ll v) { u > v ? u = v : 0; }

const int MAXN = 1e5 + 7, mod = 1e9 + 7;

static int dp[2][MAXN], n, x, a[MAXN];

inline void init() {
  read(n), read(x);
  Rep(i, 1, n) read(a[i]);
  sort(a + 1, a + n + 1, greater<int>());
}

inline void solve() {
  dp[0][x] = 1;
  Rep(i, 0, n - 1) {
    int *cr = dp[i & 1], *nx = dp[~i & 1];
    memset(nx + 1, 0, sizeof(int) * x);
    Rep(j, 1, x) if (cr[j]) {
      nx[j % a[i + 1]] = (nx[j % a[i + 1]] + cr[j]) % mod;
      nx[j] = (nx[j] + (ll)cr[j] * (n - i - 1)) % mod;
    }
  }
  static int ans = 0;
  Rep(i, 1, x) ans = (ans + (ll)i * dp[n & 1][i]) % mod;
  cout << ans << endl;
}

int main() {
  file();
  init();
  solve();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define Rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define Repe(i, a, b) for (register int i = (a); i >= (b); --i)
#define rep(i, a, b) for (register int i = (a); i < (b); ++i)
#define pb push_back
#define mp make_pair
#define mx(a, b) (a > b ? a : b)
#define mn(a, b) (a < b ? a : b)
typedef unsigned long long uint64;
typedef unsigned int uint32;
typedef long long ll;
using namespace std;

namespace IO {
const uint32 Buffsize = 1 << 15, Output = 1 << 24;
static char Ch[Buffsize], *S = Ch, *T = Ch;
inline char getc() {
  return ((S == T) && (T = (S = Ch) + fread(Ch, 1, Buffsize, stdin), S == T)
              ? 0
              : *S++);
}
static char Out[Output], *nowps = Out;

inline void flush() {
  fwrite(Out, 1, nowps - Out, stdout);
  nowps = Out;
}

template <typename T> inline void read(T &x) {
  x = 0;
  static char ch;
  T f = 1;
  for (ch = getc(); !isdigit(ch); ch = getc())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getc())
    x = x * 10 + (ch ^ 48);
  x *= f;
}

template <typename T> inline void write(T x, char ch = '\n') {
  if (!x)
    *nowps++ = '0';
  if (x < 0)
    *nowps++ = '-', x = -x;
  static uint32 sta[111], tp;
  for (tp = 0; x; x /= 10)
    sta[++tp] = x % 10;
  for (; tp; *nowps++ = sta[tp--] ^ 48)
    ;
  *nowps++ = ch;
  if (nowps - Out >= 1 << 23)
    flush();
}
} // namespace IO
using namespace IO;

void file() {
#ifndef ONLINE_JUDGE
  FILE *DSD = freopen("water.in", "r", stdin);
  FILE *CSC = freopen("water.out", "w", stdout);
#endif
}

inline void Chkmin(int &u, int v) { u > v ? u = v : 0; }

inline void Chkmax(int &u, int v) { u < v ? u = v : 0; }

inline void Chkmax(ll &u, ll v) { u < v ? u = v : 0; }

inline void Chkmin(ll &u, ll v) { u > v ? u = v : 0; }

const int MAXN = 1e5 + 7, mod = 1e9 + 7;

static int dp[2][MAXN], n, x, a[MAXN];

inline void init() {
  read(n), read(x);
  Rep(i, 1, n) read(a[i]);
  sort(a + 1, a + n + 1, greater<int>());
}

inline void solve() {
  dp[0][x] = 1;
  Rep(i, 0, n - 1) {
    int *cr = dp[i & 1], *nx = dp[~i & 1];
    memset(nx + 1, 0, sizeof(int) * x);
    Rep(j, 1, x) if (cr[j]) {
      nx[j % a[i + 1]] = (nx[j % a[i + 1]] + cr[j]) % mod;
      nx[j] = (nx[j] + (ll)cr[j] * (n - i - 1)) % mod;
    }
  }
  static int ans = 0;
  Rep(i, 1, x) ans = (ans + (ll)i * dp[n & 1][i]) % mod;
  cout << ans << endl;
}

int main() {
  init();
  solve();
  return 0;
}