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


/* You are being watched. */

//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")

using namespace std;

//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\\n"
#define FastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \\n"[sizeof...(args) == 0];
  print(args...);
}

const int N = 5e5 + 5;
const ll MOD = 1e9 + 7;

ll f[N], inv[N];

ll get(ll x, ll y) {
  if (y == -1)
    y = MOD - 2;
  ll res = 1LL;
  while (y) {
    if (y & 1)
      res = (res * x) % MOD;
    y >>= 1LL;
    x = (x * x) % MOD;
  }
  return res;
}

ll ncr(ll n, ll m) {
  if (n < 0 or m < 0 or n < m)
    return 0ll;
  return f[n] * inv[m] % MOD * inv[n - m] % MOD;
}

void solve() {
  ll n, m;
  cin >> n >> m;
  ll tobe = 0ll;
  ll mul = 1ll;
  for (ll i = 0; i <= n; i++) {
    ll cur = mul * ncr(n, i) * ncr(m - i, n - i) % MOD * f[n - i] % MOD;
    tobe = (tobe + cur) % MOD;
    if (tobe < 0)
      tobe += MOD;
    mul = -mul;
  }
  cout << (tobe * ncr(m, n) % MOD * f[n]) % MOD << '\\n';
}

int main() {
  f[0] = inv[0] = 1LL;
  for (int i = 1; i < N; i++) {
    f[i] = (i * f[i - 1]) % MOD;
    inv[i] = get(f[i], -1);
  }
  int t;
  t = 1;
  while (t--)
    solve();
}


Error: 0

Fixed submission:


/* You are being watched. */

//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")

using namespace std;

//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\\n"
#define FastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \\n"[sizeof...(args) == 0];
  print(args...);
}

const int N = 2e5 + 5;
const ll MOD = 1e9 + 7;

ll f[N], inv[N];

ll get(ll x, ll y) {
  if (y == -1)
    y = MOD - 2;
  ll res = 1LL;
  while (y) {
    if (y & 1)
      res = (res * x) % MOD;
    y >>= 1LL;
    x = (x * x) % MOD;
  }
  return res;
}

ll ncr(ll n, ll m) {
  if (n < 0 or m < 0 or n < m)
    return 0ll;
  return f[n] * inv[m] % MOD * inv[n - m] % MOD;
}

void solve() {
  ll n, m;
  cin >> n >> m;
  ll tobe = 0ll;
  ll mul = 1ll;
  for (ll i = 0; i <= n; i++) {
    ll cur = mul * ncr(n, i) * ncr(m - i, n - i) % MOD * f[n - i] % MOD;
    tobe = (tobe + cur) % MOD;
    if (tobe < 0)
      tobe += MOD;
    mul = -mul;
  }
  cout << (tobe * ncr(m, n) % MOD * f[n]) % MOD << '\\n';
}

int main() {
  f[0] = inv[0] = 1LL;
  for (int i = 1; i < N; i++) {
    f[i] = (i * f[i - 1]) % MOD;
    inv[i] = get(f[i], -1);
  }
  int t;
  t = 1;
  while (t--)
    solve();
}