prithivMLmods/Coder-Stat huggingface dataset:
Problem p03253 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
  long mod = 1000000007;

  int n = 0, m = 0;
  cin >> n;
  cin >> m;

  int mm = m;

  vector<pair<int, int>> vec;
  for (int i = 2; i * i <= mm; i++) {
    int cnt = 0;
    while (mm % i == 0) {
      cnt++;
      mm /= i;
    }
    if (cnt > 0) {
      vec.emplace_back(make_pair(i, cnt));
      // cout << i << " " << cnt << endl;
    }
  }
  if (mm > 1) {
    vec.emplace_back(make_pair(mm, 1));
    // cout << mm << " " << 1 << endl;
  }

  long ans = 1;
  for (auto p : vec) {
    long dp[2][n + 1];
    for (int i = 0; i <= n; i++) {
      dp[0][i] = 0;
      dp[1][i] = 0;
    }
    dp[0][0] = 1;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= p.second; j++) {
        for (int k = 0; k <= j; k++) {
          dp[i % 2][j] += dp[1 - (i % 2)][k];
          dp[i % 2][j] %= mod;
        }
      }
      for (int j = 0; j <= p.second; j++)
        dp[1 - (i % 2)][j] = 0;
    }
    // cout << dp[n % 2][p.second] << endl;
    ans = ans * dp[n % 2][p.second] % mod;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
  long mod = 1000000007;

  int n = 0, m = 0;
  cin >> n;
  cin >> m;

  int mm = m;

  vector<pair<int, int>> vec;
  for (int i = 2; i * i <= mm; i++) {
    int cnt = 0;
    while (mm % i == 0) {
      cnt++;
      mm /= i;
    }
    if (cnt > 0) {
      vec.emplace_back(make_pair(i, cnt));
      // cout << i << " " << cnt << endl;
    }
  }
  if (mm > 1) {
    vec.emplace_back(make_pair(mm, 1));
    // cout << mm << " " << 1 << endl;
  }

  long ans = 1;
  for (auto p : vec) {
    long dp[2][p.second + 1];
    for (int i = 0; i <= p.second; i++) {
      dp[0][i] = 0;
      dp[1][i] = 0;
    }
    dp[0][0] = 1;

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= p.second; j++) {
        for (int k = 0; k <= j; k++) {
          dp[i % 2][j] += dp[1 - (i % 2)][k];
          dp[i % 2][j] %= mod;
        }
      }
      for (int j = 0; j <= p.second; j++)
        dp[1 - (i % 2)][j] = 0;
    }
    // cout << dp[n % 2][p.second] << endl;
    ans = ans * dp[n % 2][p.second] % mod;
  }

  cout << ans << endl;

  return 0;
}
Problem p03253 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#define MOD 1000000007
#define REP(var, n) for (int var = 0; var < (n); var++)
#define REPP(var, n) for (int var = 1; var < (n); var++)
#define ALL(c) (c).begin(), (c).end()

ll memo[100100][100];
ll combination(ll n, ll r) {
  if (memo[n][r])
    return memo[n][r];
  if (r == 0 || r == n)
    return memo[n][r] = 1;
  return (combination(n - 1, r - 1) + combination(n - 1, r)) % MOD;
}

int main() {
  ll N, M, i, j;
  cin >> N >> M;

  ll p = 2;
  ll x = M;
  ll ans = 1;

  while (p * p <= M) {
    int cnt = 0;
    while (x % p == 0) {
      x /= p;
      ++cnt;
    }
    if (cnt) {
      ans = (ans * combination(N + cnt - 1, cnt)) % MOD;
    }
    ++p;
  }

  if (x != 1)
    ans = (ans * N) % MOD;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

#define MOD 1000000007
#define REP(var, n) for (int var = 0; var < (n); var++)
#define REPP(var, n) for (int var = 1; var < (n); var++)
#define ALL(c) (c).begin(), (c).end()

ll memo[100100][100];
ll combination(ll n, ll r) {
  if (memo[n][r])
    return memo[n][r];
  if (r == 0 || r == n)
    return memo[n][r] = 1;
  return memo[n][r] = (combination(n - 1, r - 1) + combination(n - 1, r)) % MOD;
}

int main() {
  ll N, M, i, j;
  cin >> N >> M;

  ll p = 2;
  ll x = M;
  ll ans = 1;

  while (p * p <= M) {
    int cnt = 0;
    while (x % p == 0) {
      x /= p;
      ++cnt;
    }
    if (cnt) {
      ans = (ans * combination(N + cnt - 1, cnt)) % MOD;
    }
    ++p;
  }

  if (x != 1)
    ans = (ans * N) % MOD;
  cout << ans << endl;

  return 0;
}
Problem p03253 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る事前計算
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

vector<int> prime_factor(int n) {
  vector<int> res;
  for (int i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res.push_back(i);
      n /= i;
    }
  }
  if (n != 1)
    res.push_back(n);
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v = prime_factor(m);
  COMinit();
  int cnt = 1, p = v[0];
  long long res = 1;
  for (int i = 1; i < v.size(); ++i) {
    if (p != v[i])
      res *= COM(n - 1 + cnt, n - 1), p = v[i], cnt = 0;
    cnt++;
    res %= MOD;
  }
  res *= COM(n - 1 + cnt, n - 1);
  res %= MOD;
  cout << res << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る事前計算
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

vector<int> prime_factor(int n) {
  vector<int> res;
  for (int i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res.push_back(i);
      n /= i;
    }
  }
  if (n != 1)
    res.push_back(n);
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  if (m == 1) {
    puts("1");
    return 0;
  }
  vector<int> v = prime_factor(m);
  COMinit();
  int cnt = 1, p = v[0];
  long long res = 1;
  for (int i = 1; i < v.size(); ++i) {
    if (p != v[i])
      res *= COM(n - 1 + cnt, n - 1), p = v[i], cnt = 0;
    cnt++;
    res %= MOD;
  }
  res *= COM(n - 1 + cnt, n - 1);
  res %= MOD;
  cout << res << endl;
}
Problem p03253 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
ll mod = 1000000007;
ll INF = 1000000099;
// cin.tie(0);
// ios::sync_with_stdio(false);

vector<pair<ll, ll>> factorize(ll x) { // mapを返す関数
  vector<pair<ll, ll>> v;
  for (ll i = 2; i * i <= x; i++) {
    ll cnt = 0;
    while (x % i == 0) {
      x /= i;
      cnt++;
    }
    v.pb({i, cnt});
  }
  if (x != 1) {
    if (x == v.back().fi)
      v.back().se++; // xが素数だった時用
    else
      v.pb({x, 1});
  }

  return v;
}

const ll MAX = 100000010; // 設定して

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod; // ＊１
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

// 二項係数計算
long long COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

signed main() {
  ll n, m, ans = 1;
  cin >> n >> m;
  vector<pair<ll, ll>> u = factorize(m);
  COMinit();

  for (ll i = 0; i < u.size(); i++) {
    ans *= COM(n - 1 + u.at(i).se, u.at(i).se);
    ans %= mod;
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
ll mod = 1000000007;
ll INF = 1000000099;
// cin.tie(0);
// ios::sync_with_stdio(false);

vector<pair<ll, ll>> factorize(ll x) { // mapを返す関数
  vector<pair<ll, ll>> v;
  for (ll i = 2; i * i <= x; i++) {
    ll cnt = 0;
    while (x % i == 0) {
      x /= i;
      cnt++;
    }
    v.pb({i, cnt});
  }
  if (x != 1) {
    if (x == v.back().fi)
      v.back().se++; // xが素数だった時用
    else
      v.pb({x, 1});
  }

  return v;
}

const ll MAX = 100100; // 設定して

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod; // ＊１
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

// 二項係数計算
long long COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

signed main() {
  ll n, m, ans = 1;
  cin >> n >> m;
  vector<pair<ll, ll>> u = factorize(m);
  COMinit();

  for (ll i = 0; i < u.size(); i++) {
    ans *= COM(n - 1 + u.at(i).se, u.at(i).se);
    ans %= mod;
  }

  cout << ans << endl;
}
Problem p03253 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define MOD 1000000007
#define MAX 1000000

using namespace std;

typedef long long ll;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

bool isPrime(ll p) {
  bool res = true;
  if (p == 1) {
    res = false;
  } else {
    for (int i = 2; i * i <= p; ++i) {
      if (p % i == 0) {
        res = false;
        break;
      }
    }
  }

  return res;
}

map<ll, int> primeFactorization(ll n) {
  map<ll, int> primeFacMap;
  ll q = 2;
  while (n != 1) {
    if (isPrime(q)) {
      while (n % q == 0) {
        n = n / q;
        if (primeFacMap.find(q) == primeFacMap.end()) {
          primeFacMap[q] = 1;
        } else {
          primeFacMap[q]++;
        }
      }
    }
    q++;
  }

  return primeFacMap;
}

int main() {
  ll N, M;
  cin >> N >> M;
  map<ll, int> primeFacMap = primeFactorization(M);
  COMinit();

  ll ans = 1;
  for (auto iter = primeFacMap.begin(); iter != primeFacMap.end(); ++iter) {
    ll b = iter->second;
    ans = (ans * COM(b + N - 1, b)) % MOD;
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define MOD 1000000007
#define MAX 1000000

using namespace std;

typedef long long ll;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

bool isPrime(ll p) {
  bool res = true;
  if (p == 1) {
    res = false;
  } else {
    for (int i = 2; i * i <= p; ++i) {
      if (p % i == 0) {
        res = false;
        break;
      }
    }
  }

  return res;
}

map<ll, int> primeFactorization(ll n) {
  map<ll, int> primeFacMap;
  ll q = 2;
  while (n != 1) {
    if (n % q == 0) {
      if (isPrime(q)) {
        while (n % q == 0) {
          n = n / q;
          if (primeFacMap.find(q) == primeFacMap.end()) {
            primeFacMap[q] = 1;
          } else {
            primeFacMap[q]++;
          }
        }
      }
    }
    q++;
  }

  return primeFacMap;
}

int main() {
  ll N, M;
  cin >> N >> M;
  map<ll, int> primeFacMap = primeFactorization(M);
  COMinit();

  ll ans = 1;
  for (auto iter = primeFacMap.begin(); iter != primeFacMap.end(); ++iter) {
    ll b = iter->second;
    ans = (ans * COM(b + N - 1, b)) % MOD;
  }

  cout << ans << endl;
}
Problem p03253 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 100;
ll f[maxn], inv[maxn];

ll powmod(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}

void init() {
  f[0] = 1;
  for (int i = 1; i < maxn; i++) {
    f[i] = f[i - 1] * i % mod;
  }
  inv[maxn - 1] = powmod(f[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 1; i--) {
    inv[i] = inv[i + 1] * (i + 1) % mod;
  }
}

ll C(int n, int m) {
  if (n == m)
    return 1;
  return f[n] * inv[m] % mod * inv[n - m] % mod;
}

ll cnt[maxn];
int tot;

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  init();
  ll n, m;
  scanf("%lld %lld", &n, &m);

  int sum = 0;
  for (int i = 2; i * i <= m; i++) {
    if (m % i)
      continue;
    sum = 0;
    while (m % i == 0) {
      sum++;
      m /= i;
    }
    cnt[++tot] = sum;
  }
  if (m != 1)
    cnt[++tot] = 1;
  ll ans = 1;
  for (int i = 1; i <= tot; i++) {
    ans = ans * C(n - 1 + cnt[i], cnt[i]) % mod;
  }
  ans = (ans % mod + mod) % mod;
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 100;
ll f[maxn], inv[maxn];

ll powmod(ll a, ll b) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}

void init() {
  f[0] = 1;
  for (int i = 1; i < maxn; i++) {
    f[i] = f[i - 1] * i % mod;
  }
  inv[maxn - 1] = powmod(f[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 1; i--) {
    inv[i] = inv[i + 1] * (i + 1) % mod;
  }
}

ll C(int n, int m) {
  if (n == m)
    return 1;
  return f[n] * inv[m] % mod * inv[n - m] % mod;
}

ll cnt[maxn];
int tot;

int main() {

  init();
  ll n, m;
  scanf("%lld %lld", &n, &m);

  int sum = 0;
  for (int i = 2; i * i <= m; i++) {
    if (m % i)
      continue;
    sum = 0;
    while (m % i == 0) {
      sum++;
      m /= i;
    }
    cnt[++tot] = sum;
  }
  if (m != 1)
    cnt[++tot] = 1;
  ll ans = 1;
  for (int i = 1; i <= tot; i++) {
    ans = ans * C(n - 1 + cnt[i], cnt[i]) % mod;
  }
  ans = (ans % mod + mod) % mod;
  printf("%lld\n", ans);
  return 0;
}
Problem p03253 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = n; i < (int)(m); i++)
#define rrep(i, n, m) for (int i = ((int)(n)-1); i >= m; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int MOD = 1000000007;
using namespace std;
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  vector<ll> k(100010, 1);
  rep(i, 1, 100010) k.at(i) = (k.at(i - 1) * i) % MOD;
  int n, m;
  cin >> n >> m;
  vector<ll> vec;
  rep(i, 2, (int)pow(m, 0.5) + 2) {
    if (m % i != 0)
      continue;
    ll cnt = 0;
    while (m % i == 0) {
      m /= i;
      cnt++;
    }
    vec.push_back(cnt);
  }
  if (m != 1)
    vec.push_back(1);

  ll ans = 1;
  for (ll r : vec) {
    ans *= k.at(r + n - 1) * modinv(k.at(r), MOD) % MOD;
    ans %= MOD;
    ans *= modinv(k.at(n - 1), MOD);
    ans %= MOD;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = n; i < (int)(m); i++)
#define rrep(i, n, m) for (int i = ((int)(n)-1); i >= m; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int MOD = 1000000007;
using namespace std;
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  vector<ll> k(200000, 1);
  rep(i, 1, 200000) k.at(i) = (k.at(i - 1) * i) % MOD;
  int n, m;
  cin >> n >> m;
  vector<ll> vec;
  rep(i, 2, (int)pow(m, 0.5) + 2) {
    if (m % i != 0)
      continue;
    ll cnt = 0;
    while (m % i == 0) {
      m /= i;
      cnt++;
    }
    vec.push_back(cnt);
  }
  if (m != 1)
    vec.push_back(1);

  ll ans = 1;
  for (ll r : vec) {
    ans *= k.at(r + n - 1) * modinv(k.at(r), MOD) % MOD;
    ans %= MOD;
    ans *= modinv(k.at(n - 1), MOD);
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p03253 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;

vector<long long> kosu; // ありえる素因数のmaxを入れる

void divisor(long long n) {
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      kosu.push_back(0);
    }
    while (n % i == 0) {
      kosu[kosu.size() - 1]++;
      n /= i;
    }
  }
  if (n != 1) {
    kosu.push_back(1);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(20) << fixed;
  long long N, M;
  cin >> N >> M;
  divisor(M);
  long long maxKosu = *max_element(kosu.begin(), kosu.end());
  vector<vector<long long>> dp(N, vector<long long>(maxKosu + 1));
  for (int i = 0; i <= maxKosu; i++)
    dp[0][i] = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= maxKosu; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j > 0) {
        dp[i][j] += dp[i][j - 1];
        dp[i][j] %= MOD;
      }
    }
  }
  long long ans = 1;
  for (int i = 0; i < kosu.size(); i++) {
    ans *= dp[N - 1][kosu[i]];
    ans %= MOD;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;

vector<long long> kosu; // ありえる素因数のmaxを入れる

void divisor(long long n) {
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      kosu.push_back(0);
    }
    while (n % i == 0) {
      kosu[kosu.size() - 1]++;
      n /= i;
    }
  }
  if (n != 1) {
    kosu.push_back(1);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(20) << fixed;
  long long N, M;
  cin >> N >> M;
  if (M == 1) {
    cout << 1 << endl;
    return 0;
  } else {
    divisor(M);
  }
  long long maxKosu = *max_element(kosu.begin(), kosu.end());
  vector<vector<long long>> dp(N, vector<long long>(maxKosu + 1));
  for (int i = 0; i <= maxKosu; i++)
    dp[0][i] = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= maxKosu; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j > 0) {
        dp[i][j] += dp[i][j - 1];
        dp[i][j] %= MOD;
      }
    }
  }
  long long ans = 1;
  for (int i = 0; i < kosu.size(); i++) {
    ans *= dp[N - 1][kosu[i]];
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p03253 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
long long mod = 1e9 + 7;

long long modpow(long long a, long long p) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    long long halfP = p / 2;
    long long half = modpow(a, halfP);
    return half * half % mod;
  } else {
    return a * modpow(a, p - 1) % mod;
  }
}

long long comb(int a, int b) {
  if (b > a - b)
    return comb(a, a - b);
  long long ansMul = 1;
  long long ansDiv = 1;
  for (int i = 0; i < b; i++) {
    ansMul *= (a - i);
    ansDiv *= (i + 1);
    ansMul %= mod;
    ansDiv %= mod;
  }
  long long ans = ansMul * modpow(ansDiv, mod - 2) % mod;
  return ans;
}

int main() {
  long long n, m;
  cin >> n >> m;
  long long ans = 1;
  for (int i = 2; i < m; i++) {
    long long cnt = 0;
    while (m % i == 0) {
      cnt++;
      m /= i;
    }
    ans *= comb(n + cnt - 1, n - 1);
    ans %= mod;
  }
  if (m != 1) {
    ans *= comb(n, n - 1);
    ans %= mod;
  }
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
long long mod = 1e9 + 7;

long long modpow(long long a, long long p) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    long long halfP = p / 2;
    long long half = modpow(a, halfP);
    return half * half % mod;
  } else {
    return a * modpow(a, p - 1) % mod;
  }
}

long long comb(int a, int b) {
  if (b > a - b)
    return comb(a, a - b);
  long long ansMul = 1;
  long long ansDiv = 1;
  for (int i = 0; i < b; i++) {
    ansMul *= (a - i);
    ansDiv *= (i + 1);
    ansMul %= mod;
    ansDiv %= mod;
  }
  long long ans = ansMul * modpow(ansDiv, mod - 2) % mod;
  return ans;
}

int main() {
  long long n, m;
  cin >> n >> m;
  long long ans = 1;
  for (int i = 2; i < m && i < 1e5; i++) {
    long long cnt = 0;
    while (m % i == 0) {
      cnt++;
      m /= i;
    }
    ans *= comb(n + cnt - 1, n - 1);
    ans %= mod;
  }
  if (m != 1) {
    ans *= comb(n, n - 1);
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p03253 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAX 100005
#define pb push_back
#define INF 2147483647
#define MOD 1000000007
using namespace std;

typedef long long ll;

vector<int> factorize(int n) {
  vector<int> v;
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      v.pb(i);
      n /= i;
    }
  }

  if (n != 1)
    v.pb(n);
  return v;
}

ll powMod(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }

  return res;
}

ll fact(int n) {
  ll res = 1;
  for (int i = 1; i <= n; i++) {
    res = (res * i) % MOD;
  }

  return res;
}

ll solve(int n, int r) {
  ll res = 1;
  n = n + r - 1;
  for (int i = n; i >= n - r + 1; i--) {
    res = (1LL * res * i) % MOD;
  }
  res = (res * powMod(fact(r), MOD - 2)) % MOD;
  return res;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, m;

  cin >> n >> m;

  map<ll, ll> mp;
  vector<int> factor = factorize(m);

  for (int i = 0; i < factor.size(); i++) {
    mp[factor[i]]++;
  }

  ll ans = 1;
  for (map<ll, ll>::iterator it = mp.begin(); it != mp.end(); it++) {
    ans = (ans * solve(n, it->second)) % MOD;
    // cout<<solve(n, it->second)<<endl;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAX 100005
#define pb push_back
#define INF 2147483647
#define MOD 1000000007
using namespace std;

typedef long long ll;

vector<int> factorize(int n) {
  vector<int> v;
  for (int i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      v.pb(i);
      n /= i;
    }
  }

  if (n != 1)
    v.pb(n);
  return v;
}

ll powMod(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }

  return res;
}

ll fact(int n) {
  ll res = 1;
  for (int i = 1; i <= n; i++) {
    res = (res * i) % MOD;
  }

  return res;
}

ll solve(int n, int r) {
  ll res = 1;
  n = n + r - 1;
  for (int i = n; i >= n - r + 1; i--) {
    res = (1LL * res * i) % MOD;
  }
  res = (res * powMod(fact(r), MOD - 2)) % MOD;
  return res;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, m;

  cin >> n >> m;

  map<ll, ll> mp;
  vector<int> factor = factorize(m);

  for (int i = 0; i < factor.size(); i++) {
    mp[factor[i]]++;
  }

  ll ans = 1;
  for (map<ll, ll>::iterator it = mp.begin(); it != mp.end(); it++) {
    ans = (ans * solve(n, it->second)) % MOD;
    // cout<<solve(n, it->second)<<endl;
  }

  cout << ans << endl;

  return 0;
}
Problem p03253 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define Would
#define you
#define please

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  int A[100000] = {};

  int RT = pow(M, 0.5);
  for (int i = 2; i <= RT; i++) {
    while (M % i == 0) {
      A[i]++;
      M /= i;
    }
  }
  if (M != 1)
    A[M]++;

  ll pasu[100100][35] = {};
  ll mod = 1e9 + 7;

  rep1(i, N) {
    pasu[i][0] = 1;
    rep1(ii, 34) {
      if (i == 1)
        pasu[i][ii] = 1;
      else
        pasu[i][ii] = (pasu[i][ii - 1] + pasu[i - 1][ii]) % mod;
    }
  }

  ll kotae = 1;
  rep(i, 100000) { kotae = (kotae * pasu[N][A[i]]) % mod; }

  co(kotae);

  Would you please return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define Would
#define you
#define please

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  int A[100000] = {};

  int RT = pow(M, 0.5);
  for (int i = 2; i <= RT; i++) {
    while (M % i == 0) {
      A[i]++;
      M /= i;
    }
  }
  if (M != 1)
    A[1]++;

  ll pasu[100100][35] = {};
  ll mod = 1e9 + 7;

  rep1(i, N) {
    pasu[i][0] = 1;
    rep1(ii, 34) {
      if (i == 1)
        pasu[i][ii] = 1;
      else
        pasu[i][ii] = (pasu[i][ii - 1] + pasu[i - 1][ii]) % mod;
    }
  }

  ll kotae = 1;
  rep(i, 100000) { kotae = (kotae * pasu[N][A[i]]) % mod; }

  co(kotae);

  Would you please return 0;
}
Problem p03253 (Python)

Buggy submission (status: Runtime Error):
from math import sqrt
from scipy.special import comb

n, m = map(int, input().split())
mod = 10**9 + 7


def sieve(x):
    primes = []
    li = list(range(2, x + 1))

    while li[0] <= sqrt(x):
        p = li[0]
        primes.append(p)
        li = [e for e in li if e % p != 0]

    primes += li
    return primes


def prime_fact(x):
    primes = sieve(int(sqrt(x)) + 1)
    d = {}

    for p in primes:
        cnt = 0
        while x % p == 0:
            x //= p
            cnt += 1

        if cnt:
            d[p] = cnt

    if x != 1:
        d[x] = 1

    return d


pf = prime_fact(m)
ans = 1
for v in pf.values():
    ans *= comb(v + n - 1, v, exact=True)
    ans %= mod

print(ans)


Fixed submission:
from math import sqrt
from scipy.misc import comb

n, m = map(int, input().split())
mod = 10**9 + 7


def sieve(x):
    primes = []
    li = list(range(2, x + 1))

    while li[0] <= sqrt(x):
        p = li[0]
        primes.append(p)
        li = [e for e in li if e % p != 0]

    primes += li
    return primes


def prime_fact(x):
    primes = sieve(int(sqrt(x)) + 1)
    d = {}

    for p in primes:
        cnt = 0
        while x % p == 0:
            x //= p
            cnt += 1

        if cnt:
            d[p] = cnt

    if x != 1:
        d[x] = 1

    return d


pf = prime_fact(m)
ans = 1
for v in pf.values():
    ans *= comb(v + n - 1, v, exact=True)
    ans %= mod

print(ans)
Problem p03254 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN
int n, x, a[MAXN + 5], cnt;
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (x >= a[i])
      x -= a[i], cnt++;
    else
      break;
  if (cnt == n && x)
    cnt--;
  printf("%d", cnt);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 100
int n, x, a[MAXN + 5], cnt;
int main() {
  scanf("%d%d", &n, &x);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (x >= a[i])
      x -= a[i], cnt++;
    else
      break;
  if (cnt == n && x)
    cnt--;
  printf("%d", cnt);
}
Problem p03254 (C++)

Buggy submission (status: Runtime Error):
/* it was worth becoming a chemist */
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned int uni;
typedef unsigned long long unll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<int> vi;
typedef vector<long long> vll;

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define NAME "puts"
#define F first
#define S second

const ll INF = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
const db EPS = (db)1e-9;
const db pi = acos(-1.0);
const int MAXN = 2e6 + 5;

int n;
ll a[MAXN], x;

int main() {
#ifndef ONLINE_JUDGE
  freopen(NAME ".in", "r", stdin);
  freopen(NAME ".out", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(9) << fixed;

  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 1 + n);
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (i == n) {
      if (a[i] == x)
        res++;
      continue;
    }
    if (a[i] <= x) {
      x -= a[i];
      res++;
    }
  }
  cout << res;
  return 0;
}


Fixed submission:
/* it was worth becoming a chemist */
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned int uni;
typedef unsigned long long unll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<int> vi;
typedef vector<long long> vll;

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define NAME "puts"
#define F first
#define S second

const ll INF = 1e18;
const int inf = 1e9;
const int mod = 1e9 + 7;
const db EPS = (db)1e-9;
const db pi = acos(-1.0);
const int MAXN = 2e6 + 5;

int n;
ll a[MAXN], x;

int main() {

  cin >> n >> x;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 1 + n);
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (i == n) {
      if (a[i] == x)
        res++;
      continue;
    }
    if (a[i] <= x) {
      x -= a[i];
      res++;
    }
  }
  cout << res;
  return 0;
}
Problem p03254 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using p = pair<int, int>;
typedef vector<int> vi;

int main() {
  int N, x;
  cin >> N >> x;
  vi a;
  rep(i, N) cin >> a.at(i);
  sort(a.begin(), a.end());

  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (x >= a[i]) {
      ++ans;
      x -= a[i];
    }
  }

  if (ans == N && x > 0)
    --ans;

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using p = pair<int, int>;
typedef vector<int> vi;

int main() {
  int N, x;
  cin >> N >> x;
  int a[N];
  rep(i, N) cin >> a[i];
  sort(a, a + N);

  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (x >= a[i]) {
      ++ans;
      x -= a[i];
    }
  }

  if (ans == N && x > 0)
    --ans;

  cout << ans << endl;

  return 0;
}
Problem p03254 (C++)

Buggy submission (status: Runtime Error):
// IOI 2021
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 100;

int n, a[MAXN], k, ans;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (k >= a[i])
      k -= a[i], ans++;
    else
      k = 0;
  cout << ans - (k > 0) << endl;
  return 0;
}


Fixed submission:
// IOI 2021
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 100 + 17;

int n, a[MAXN], k, ans;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++)
    if (k >= a[i])
      k -= a[i], ans++;
    else
      k = 0;
  cout << ans - (k > 0) << endl;
  return 0;
}
Problem p03254 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  int k;
  int a[n];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int nu;
    cin >> nu;
    a[i] = nu;
  }
  sort(a, a + n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (k < a[i]) {
      break;
    }
    if (k >= a[i]) {
      k -= a[i];
      ans++;
    }
    if (i == n - 1 & k != 0) {
      ans--;
    }
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  int k;
  int a[n];
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (k < a[i]) {
      break;
    }
    if (k >= a[i]) {
      k -= a[i];
      ans++;
    }
    if (i == n - 1 & k != 0) {
      ans--;
    }
  }
  cout << ans;
}
Problem p03254 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FORR(i, n, m) for (int i = n - 1; i >= m; i--)
#define all(in) in.begin(), in.end()
#define ALL(in, K) in, in + K
#define INF 1e18
#define MOD 100000007
#define SIZE 100005
#define PI 3.14159265358979323846

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, x;
  cin >> n >> x;
  ll a[10];
  REP(i, n) cin >> a[i];
  sort(a, a + n);
  int i;
  for (i = 0; i < n; i++) {
    x -= a[i];
    if (x < 0)
      break;
  }
  // cout << i << endl;
  // if(i==n)i--;
  if (x > 0)
    i--;
  cout << i << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FORR(i, n, m) for (int i = n - 1; i >= m; i--)
#define all(in) in.begin(), in.end()
#define ALL(in, K) in, in + K
#define INF 1e18
#define MOD 100000007
#define SIZE 100005
#define PI 3.14159265358979323846

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, x;
  cin >> n >> x;
  ll a[1000];
  REP(i, n) cin >> a[i];
  sort(a, a + n);
  int i;
  for (i = 0; i < n; i++) {
    x -= a[i];
    if (x < 0)
      break;
  }
  // cout << i << endl;
  // if(i==n)i--;
  if (x > 0)
    i--;
  cout << i << endl;
  return 0;
}
Problem p03254 (C++)

Buggy submission (status: Runtime Error):
/*
author: Apoorv Singh
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define ll long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define watch(x) cout << (#x) << " " << (x) << endl;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second

ll modulo = 1e9 + 7;

template <class X>

void printarr(X arr[], int n) {
  for (int i = 0; i < n; ++i)
    cout << arr[i] << " ";
  cout << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, x;
  cin >> n >> x;
  int arr[n];
  FOR(i, 0, n)
  cin >> arr[i];
  sort(arr, arr + n);
  int cnt = 0, ans = 0;
  while (x >= arr[cnt] and cnt < n - 1) {
    x -= arr[cnt];
    ++cnt;
    ++ans;
  }
  if (arr[n - 1] == x)
    ++ans;
  cout << ans << endl;
  return 0;
}


Fixed submission:
/*
author: Apoorv Singh
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define ll long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define watch(x) cout << (#x) << " " << (x) << endl;
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second

ll modulo = 1e9 + 7;

template <class X>

void printarr(X arr[], int n) {
  for (int i = 0; i < n; ++i)
    cout << arr[i] << " ";
  cout << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, x;
  cin >> n >> x;
  int arr[n];
  FOR(i, 0, n)
  cin >> arr[i];
  sort(arr, arr + n);
  int cnt = 0, ans = 0;
  while (x >= arr[cnt] and cnt < n - 1) {
    x -= arr[cnt];
    ++cnt;
    ++ans;
  }
  if (arr[n - 1] == x)
    ++ans;
  cout << ans << endl;
  return 0;
}
Problem p03254 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int memo[100];
int dp(int now, int sum, int ans);
int n, x, a[100];

int main() {
  fill(memo, memo + 100, -1);
  scanf("%d %d", &n, &x);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  sort(a, a + n);
  printf("%d\n", dp(0, 0, 0));

  return 0;
}

int dp(int now, int sum, int ans) {
  if (now == n) {
    if (sum < x)
      return ans - 1;
    else
      return ans;
  }
  if (sum + a[now] > x)
    return ans;
  return memo[now] =
             max(dp(now + 1, sum, ans), dp(now + 1, sum + a[now], ans + 1));
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int memo[100];
int dp(int now, int sum, int ans);
int n, x, a[100];

int main() {
  fill(memo, memo + 100, -1);
  scanf("%d %d", &n, &x);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  sort(a, a + n);
  printf("%d\n", dp(0, 0, 0));

  return 0;
}

int dp(int now, int sum, int ans) {
  if (now == n) {
    if (sum < x)
      return ans - 1;
    else
      return ans;
  }
  if (memo[now] != -1)
    return ans;
  if (sum + a[now] > x)
    return ans;
  return memo[now] =
             max(dp(now + 1, sum, ans), dp(now + 1, sum + a[now], ans + 1));
}
Problem p03255 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define mp make_pair
using namespace std;
using ll = unsigned long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll LINF = 1LL << 62;
const double eps = 1e-5;
template <typename T> void chmin(T &a, T b) { a = min(a, b); };
template <typename T> void chmax(T &a, T b) { a = max(a, b); };
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  ll n, X;
  cin >> n >> X;
  vector<ll> x(n + 10), sum(n + 10);
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    sum[i] = sum[i - 1] + x[i];
  }
  ll ans = LINF;
  for (ll k = 1; k <= n; k++) {
    ll s = 0, c = 1;
    s += (sum[n % k] - sum[0]) * (n / k + c + 1ULL) * (n / k + c + 1ULL);
    for (ll i = n % k + k; i <= n; i += k) {
      --c;
      s += (sum[i] - sum[i - k] - sum[i - k] + sum[max(0ULL, i - 2 * k)]) *
           (n / k + c + 1LL) * (n / k + c + 1LL);
    }
    s += (n + k) * X;
    s += sum[n] - sum[n - k];
    ans = min(ans, s);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define mp make_pair
using namespace std;
using ll = unsigned long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll LINF = 1LL << 62;
const double eps = 1e-5;
template <typename T> void chmin(T &a, T b) { a = min(a, b); };
template <typename T> void chmax(T &a, T b) { a = max(a, b); };
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  ll n, X;
  cin >> n >> X;
  vector<ll> x(n + 10), sum(n + 10);
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
    sum[i] = sum[i - 1] + x[i];
  }
  ll ans = LINF;
  for (ll k = 1; k <= n; k++) {
    ll s = 0, c = 1;
    s += (sum[n % k] - sum[0]) * (n / k + c + 1ULL) * (n / k + c + 1ULL);
    for (ll i = n % k + k; i <= n; i += k) {
      --c;
      ll id = 0;
      if (i > 2 * k)
        id = i - 2 * k;
      s += (sum[i] - sum[i - k] - sum[i - k] + sum[id]) * (n / k + c + 1LL) *
           (n / k + c + 1LL);
    }
    s += (n + k) * X;
    s += sum[n] - sum[n - k];
    ans = min(ans, s);
  }
  cout << ans << endl;
}
Problem p03255 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define SF scanf
#define PF printf
#define MAXN 100010
using namespace std;
typedef long long ll;
ll a[MAXN], x, pre[MAXN], ans;
int n;
int main() {
  SF("%d%lld", &n, &x);
  for (int i = 1; i <= n; i++) {
    SF("%lld", &a[i]);
    pre[i] = pre[i - 1] + a[i];
    ans += a[i] * 5ll + 2ll * x;
  }
  for (int k = 1; k < n; k++) {
    ll res = k * x;
    for (int j = n, cnt = 1; j > 0; cnt++) {
      int j1 = max(0, j - k);
      ll sum = pre[j] - pre[j1];
      res += max(5ll, cnt * 2ll + 1ll) * sum;
      if (res > ans)
        break;
      j = j1;
    }
    ans = min(ans, res);
  }
  PF("%lld", ans + 1ll * n * x);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#define SF scanf
#define PF printf
#define MAXN 200010
using namespace std;
typedef long long ll;
ll a[MAXN], x, pre[MAXN], ans;
int n;
int main() {
  SF("%d%lld", &n, &x);
  for (int i = 1; i <= n; i++) {
    SF("%lld", &a[i]);
    pre[i] = pre[i - 1] + a[i];
    ans += a[i] * 5ll + 2ll * x;
  }
  for (int k = 1; k < n; k++) {
    ll res = k * x;
    for (int j = n, cnt = 1; j > 0; cnt++) {
      int j1 = max(0, j - k);
      ll sum = pre[j] - pre[j1];
      res += max(5ll, cnt * 2ll + 1ll) * sum;
      if (res > ans)
        break;
      j = j1;
    }
    ans = min(ans, res);
  }
  PF("%lld", ans + 1ll * n * x);
}
Problem p03255 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int unsigned long long
int sum[200003], N, X;

signed main() {
  cin >> N >> X;
  int ans = 1e19;
  for (int i = 1; i <= N; ++i) {
    cin >> sum[i];
    sum[i] += sum[i - 1];
  }
  for (int j = 1; j <= N; ++j) {
    int pos = N, all = j * X, cur = 3;
    while (pos > 0) {
      all += (cur == 3 ? 5 : cur) * (sum[pos] - sum[max(0ull, pos - j)]);
      pos -= j;
      cur += 2;
    }
    ans = min(ans, all);
  }
  cout << ans + N * X << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int unsigned long long
int sum[200003], N, X;

signed main() {
  cin >> N >> X;
  int ans = 1e19;
  for (int i = 1; i <= N; ++i) {
    cin >> sum[i];
    sum[i] += sum[i - 1];
  }
  for (int j = 1; j <= N; ++j) {
    int pos = N, all = j * X, cur = 3;
    while (pos > 0) {
      all += (cur == 3 ? 5 : cur) * (sum[pos] - sum[max(j, pos) - j]);
      pos = max(pos, j) - j;
      cur += 2;
    }
    ans = min(ans, all);
  }
  cout << ans + N * X << endl;
  return 0;
}
Problem p03255 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ll;

const int N = 100100;

ll x[N];
int n;
ll c;

ll calc(int k) {
  ll s = (n + k - 1) * c;
  for (int i = n - 1; i >= 1; --i) {
    int j = n - i + 1;
    int t = j / 2;
    t = min(t, k);
    j += t - 1;
    ll k1 = j / t;
    ll k2 = k1 + 1;
    k1 *= k1;
    k2 *= k2;
    ll m2 = j % t;
    ll m1 = t - j % t;
    s += (x[i] - x[i - 1]) * (k1 * m1 + k2 * m2 + t);
  }
  return s;
}

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // uniform_int_distribution<int>(l, r)(rng);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> c;
  ++n;
  x[0] = 0;
  for (int i = 1; i < n; ++i) {
    cin >> x[i];
  }
  ll ans = 0;
  int l = 1, r = n;
  while (r - l > 0) {
    int t = (r - l) / 3;
    int m1 = l + t;
    int m2 = r - t;
    ll t1 = calc(m1);
    ll t2 = calc(m2);
    if (r - l == 1) {
      ans = min(t1, t2);
      break;
    }
    if (r - l == 2) {
      ll t3 = calc(l + 1);
      ans = min(t1, t2);
      ans = min(ans, t3);
      break;
    }
    if (t1 > t2) {
      l = m1;
    } else {
      r = m2;
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ll;

const int N = 200100;

ll x[N];
int n;
ll c;

ll calc(int k) {
  ll s = (n + k - 1) * c;
  for (int i = n - 1; i >= 1; --i) {
    int j = n - i + 1;
    int t = j / 2;
    t = min(t, k);
    j += t - 1;
    ll k1 = j / t;
    ll k2 = k1 + 1;
    k1 *= k1;
    k2 *= k2;
    ll m2 = j % t;
    ll m1 = t - j % t;
    s += (x[i] - x[i - 1]) * (k1 * m1 + k2 * m2 + t);
  }
  return s;
}

int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  // uniform_int_distribution<int>(l, r)(rng);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> c;
  ++n;
  x[0] = 0;
  for (int i = 1; i < n; ++i) {
    cin >> x[i];
  }
  ll ans = 0;
  int l = 1, r = n;
  while (r - l > 0) {
    int t = (r - l) / 3;
    int m1 = l + t;
    int m2 = r - t;
    ll t1 = calc(m1);
    ll t2 = calc(m2);
    if (r - l == 1) {
      ans = min(t1, t2);
      break;
    }
    if (r - l == 2) {
      ll t3 = calc(l + 1);
      ans = min(t1, t2);
      ans = min(ans, t3);
      break;
    }
    if (t1 > t2) {
      l = m1;
    } else {
      r = m2;
    }
  }
  cout << ans;
  return 0;
}