alexjercan/bugnet huggingface dataset:
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 998244353;
const int maxn = 2e5 + 100;
LL num[maxn];
LL fac[maxn], Inv[maxn], p[maxn];
LL quick_pow(LL x, LL n, LL p) // 快速幂求x^n mod p 的结果
{
  if (n == 0)
    return 1;
  if (n == 1)
    return x % p;
  LL ans = 1;
  LL tmp = x % p;
  while (n) {
    if (n & 1) {
      ans = (ans * tmp) % p;
    }
    tmp = tmp * tmp % p;
    n >>= 1;
  }
  return ans % p;
}
LL inv(LL b, LL p) // 求数 b mod p 的逆元
{
  return quick_pow(b, p - 2, p);
}
LL C(LL n, LL m, LL p) // 组合数取模
{
  if (m == 0 || m == n)
    return 1;
  if (m == 1 || m == n - 1)
    return n % p;
  m = min(m, n - m);
  if (num[m] != -1)
    return num[m];
  LL up = 1, down = 1;
  for (LL i = n - m + 1; i <= n; i++)
    up = (up * i) % p;
  for (LL i = 1; i <= m; i++)
    down = (down * i) % p;
  up %= p;
  down %= p;
  return num[m] = (up * (inv(down, p) % p)) % p;
}
LL lucas(LL n, LL m, LL p) // 递归lucas函数
{
  if (m == 0)
    return 1;
  return lucas(n / p, m / p, p) * C(n % p, m % p, p) % p;
}
LL C1(LL n, LL k) { return fac[n] * Inv[k] % mod * Inv[n - k] % mod; }
int main() {
  LL n, m, k;
  cin >> n >> m >> k;
  LL ans = 0;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i)
    fac[i] = 1ll * fac[i - 1] * i % mod;
  Inv[n] = inv(fac[n], mod);
  for (int i = n; i > 0; --i) {
    Inv[i - 1] = 1ll * Inv[i] * i % mod;
  }
  p[0] = 1;
  for (int i = 1; i <= n; ++i)
    p[i] = p[i - 1] * (m - 1) % mod;

  for (LL i = 0; i <= k; ++i) {
    ans = (ans + C1(n - 1, i) % mod * p[n - i - 1] % mod) % mod;
  }
  ans = (ans * m) % mod;
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 998244353;
const int maxn = 2e5 + 100;
LL num[maxn];
LL quick_pow(LL x, LL n, LL p) // 快速幂求x^n mod p 的结果
{
  if (n == 0)
    return 1;
  if (n == 1)
    return x % p;
  LL ans = 1;
  LL tmp = x % p;
  while (n) {
    if (n & 1) {
      ans = (ans * tmp) % p;
    }
    tmp = tmp * tmp % p;
    n >>= 1;
  }
  return ans % p;
}
LL inv(LL b, LL p) // 求数 b mod p 的逆元
{
  return quick_pow(b, p - 2, p);
}
LL C(LL n, LL m, LL p) // 组合数取模
{
  if (m == 0 || m == n)
    return 1;
  if (m == 1 || m == n - 1)
    return n % p;
  m = min(m, n - m);
  if (num[m] != -1)
    return num[m];
  LL up = 1, down = 1;
  for (LL i = n - m + 1; i <= n; i++)
    up = (up * i) % p;
  for (LL i = 1; i <= m; i++)
    down = (down * i) % p;
  up %= p;
  down %= p;
  return num[m] = (up * (inv(down, p) % p)) % p;
}
LL lucas(LL n, LL m, LL p) // 递归lucas函数
{
  if (m == 0)
    return 1;
  return lucas(n / p, m / p, p) * C(n % p, m % p, p) % p;
}

int main() {
  LL n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i <= n; ++i)
    num[i] = -1;
  LL ans = 0;
  for (LL i = 0; i <= k; ++i) {
    ans = (ans +
           (lucas(n - 1, i, mod) * quick_pow(m - 1, n - i - 1, mod)) % mod) %
          mod;
  }
  ans = (ans * m) % mod;
  cout << ans << "\\n";
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define MOD 998244353
const int INF = 1LL << 30;

void add(int64_t &a, int64_t b) { a = (a + b) % MOD; }
void mul(int64_t &a, int64_t b) { a = a * b % MOD; }

int64_t power_mod(int64_t num, int64_t power) {
  int64_t prod = 1;
  num %= MOD;
  while (power > 0) {
    if (power & 1)
      prod = prod * num % MOD;
    num = num * num % MOD;
    power >>= 1;
  }
  return prod;
}

int64_t extgcd(int64_t a, int64_t b, int64_t &x, int64_t &y) {
  int64_t d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

int64_t inv_mod(int64_t a) {
  int64_t x, y;
  extgcd(a, MOD, x, y);
  return (MOD + x % MOD) % MOD;
}

vector<int64_t> fact, fact_inv;

void create_mod_tables(int num) {
  fact.assign(num + 1, 1);
  fact_inv.assign(num + 1, 1);
  for (int i = 1; i <= num; i++)
    fact[i] = fact[i - 1] * i % MOD;
  fact_inv[num] = inv_mod(fact[num]);
  for (int i = num; i > 0; i--)
    fact_inv[i - 1] = fact_inv[i] * i % MOD;
}

int64_t comb_mod(int n, int k) {
  return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD;
}

int64_t perm_mod(int n, int k) { return fact[n] * fact_inv[n - k] % MOD; }

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  create_mod_tables(300000);

  int64_t ans = 0;
  for (int i = 0; i <= K; i++) {
    int64_t res = comb_mod(N - 1, i);
    int num = N - i;
    mul(res, M);
    mul(res, power_mod(M - 1, num - 1));
    add(ans, res);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define MOD 998244353
const int INF = 1LL << 30;

int N, M, K;

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

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < 100010; 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;
}

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod = MOD) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cin >> N >> M >> K;

  if (N == 1) {
    cout << M << endl;
    return 0;
  }

  if (M == 1) {
    if (N - 1 == K)
      cout << M << endl;
    else
      cout << 0 << endl;
    return 0;
  }

  COMinit();
  ll ans = 0;
  rep(k, K + 1) {
    ll tmp1 = COM(N - 1, k);
    ll tmp2 = modpow(M - 1, N - 1 - k);
    ll add = (tmp1 * M) % MOD;
    add = (add * tmp2) % MOD;
    ans = (ans + add) % MOD;
  }

  cout << ans << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b) {
    return a.first < b.first;
}
use case
sort(V.begin(), V.end(), compare_function);*/

ll laip(ll a, ll b, ll liek) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a % liek;
  }
  ll c = (a * a) % liek;
  if (b % 2 == 0) {
    return laip(c, b / 2, liek);
  }
  return (a * laip(c, b / 2, liek)) % liek;
}

ll cnk(ll n, ll i, ll liek, vector<ll> &F) {
  ll x = F[n] % liek;
  // cout<<n<<" "<<x<<endl;
  ll y = F[i] * F[n - i];
  y = y % liek;
  // cout<<i<<" "<<n-i<<" "<<x<<" "<<y<<endl;
  ll ats = x * laip(y, liek - 2, liek);
  ats = ats % liek;
  return ats;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  ll liek = 998244353;
  vector<ll> F;
  F.push_back(1);
  for (int i = 0; i < 200005; i++) {
    ll x = (i + 1) * F[i];
    x = x % liek;
    F.push_back(x);
  }
  // cout<<cnk(3,2,liek,F)<<endl;
  ll ats = 0;
  for (int i = 0; i <= k; i++) {
    ll x = cnk(n - 1, i, liek, F) * m;
    // cout<<x<<endl;
    x = x % liek;
    x = x * laip(m - 1, n - i - 1, liek);
    x = x % liek;
    // cout<<i<<" "<<x<<endl;
    ats = ats + x;
    ats = ats % liek;
  }
  cout << ats << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b) {
    return a.first < b.first;
}
use case
sort(V.begin(), V.end(), compare_function);*/

ll laip(ll a, ll b, ll liek) {
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a % liek;
  }
  ll c = (a * a) % liek;
  if (b % 2 == 0) {
    return laip(c, b / 2, liek);
  }
  return (a * laip(c, b / 2, liek)) % liek;
}

ll cnk(ll n, ll i, ll liek, vector<ll> &F) {
  ll x = F[n] % liek;
  // cout<<n<<" "<<x<<endl;
  ll y = F[i] * F[n - i];
  y = y % liek;
  // cout<<i<<" "<<n-i<<" "<<x<<" "<<y<<endl;
  ll ats = x * laip(y, liek - 2, liek);
  ats = ats % liek;
  return ats;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, m, k;
  cin >> n >> m >> k;
  ll liek = 998244353;
  vector<ll> F;
  F.push_back(1);
  for (int i = 0; i < 100005; i++) {
    ll x = (i + 1) * F[i];
    x = x % liek;
    F.push_back(x);
  }
  // cout<<cnk(3,2,liek,F)<<endl;
  ll ats = 0;
  for (int i = 0; i <= k; i++) {
    ll x = cnk(n - 1, i, liek, F) * m;
    // cout<<x<<endl;
    x = x % liek;
    x = x * laip(m - 1, n - i - 1, liek);
    x = x % liek;
    // cout<<i<<" "<<x<<endl;
    ats = ats + x;
    ats = ats % liek;
  }
  cout << ats << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define LL_MAX LLONG_MAX
#define ULL_MAX ULLONG_MAX
#define ull unsigned long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define mk make_pair
#define endl '\\n'

const ll M = 998244353;

ll f[200001];

ll pow(ll val, ll exp, ll MOD) {
  ll x = 1, y = val;
  while (exp > 0) {
    if (exp % 2 == 1) {
      x = (x * y);
      if (x > MOD)
        x %= MOD;
    }
    y = (y * y);
    if (y > MOD)
      y %= MOD;
    exp /= 2;
  }
  return x;
}
ll InverseEuler(ll n, ll MOD) { return pow(n, MOD - 2, MOD); }

ll C(ll n, ll r, ll MOD) {
  return (f[n] *
          ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) %
         MOD;
}

void compute(int mod) {
  f[0] = 1;
  for (int i = 1; i <= 200000; i++)
    f[i] = (f[i - 1] * i) % mod;
}

int main() {
  IOS;
  compute(M);
  int n, m, k;
  cin >> n >> m >> k;
  int ans = 0;
  for (int i = 0; i <= k; i++) {
    ll temp = pow(m - 1, n - 1 - i, M) * m;
    temp %= M;
    temp *= C(n - 1, i, M);
    temp %= M;
    ans += temp;
    ans %= M;
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define LL_MAX LLONG_MAX
#define ULL_MAX ULLONG_MAX
#define ull unsigned long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define mk make_pair
#define endl '\\n'

const ll M = 998244353;

ll f[100001];

ll pow(ll val, ll exp, ll MOD) {
  ll x = 1, y = val;
  while (exp > 0) {
    if (exp % 2 == 1) {
      x = (x * y);
      if (x > MOD)
        x %= MOD;
    }
    y = (y * y);
    if (y > MOD)
      y %= MOD;
    exp /= 2;
  }
  return x;
}
ll InverseEuler(ll n, ll MOD) { return pow(n, MOD - 2, MOD); }

ll C(ll n, ll r, ll MOD) {
  return (f[n] *
          ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) %
         MOD;
}

void compute(int mod) {
  f[0] = 1;
  for (int i = 1; i <= 100000; i++)
    f[i] = (f[i - 1] * i) % mod;
}

int main() {
  IOS;
  compute(M);
  int n, m, k;
  cin >> n >> m >> k;
  int ans = 0;
  for (int i = 0; i <= k; i++) {
    ll temp = pow(m - 1, n - 1 - i, M) * m;
    temp %= M;
    temp *= C(n - 1, i, M);
    temp %= M;
    ans += temp;
    ans %= M;
  }
  cout << ans;
}
Problem p02685 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int mod = 998244353;
const int inf = 2e9 + 5;
const int N = 2e5 + 5;

ll n, m, k, fact[N];

ll modexp(ll x, int n) {
  if (n == 0)
    return 1;
  ll ans = modexp((x * x) % mod, n / 2);
  if (n % 2 != 0)
    return (x * ans) % mod;
  return ans;
}

ll modinv(int x) { return modexp(x, mod - 2); }

ll choose(int i, int j) {
  ll num1 = fact[i];
  ll num2 = (fact[i - j] * fact[j]) % mod;
  num2 = modinv(num2);

  return (num1 * num2) % mod;
}

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

  cin >> n >> m >> k;

  fact[0] = 1;
  for (int i = 1; i <= N - 5; i++)
    fact[i] = (i * fact[i - 1]) % mod;

  if (k == 0) {
    cout << (m * modexp(m - 1, n - 1)) % mod;
    return 0;
  }

  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ans = (ans +
           (choose(n - 1, i) * ((m * modexp(m - 1, n - 1 - i)) % mod)) % mod) %
          mod;
  }

  cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int mod = 998244353;
const int inf = 2e9 + 5;
const int N = 2e5 + 5;

ll n, m, k, fact[N];

ll modexp(ll x, int n) {
  if (n == 0)
    return 1;
  ll ans = modexp((x * x) % mod, n / 2);
  if (n % 2 != 0)
    return (x * ans) % mod;
  return ans;
}

ll modinv(int x) { return modexp(x, mod - 2); }

ll choose(int i, int j) {
  ll num1 = fact[i];
  ll num2 = (fact[i - j] * fact[j]) % mod;
  num2 = modinv(num2);

  return (num1 * num2) % mod;
}

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

  cin >> n >> m >> k;

  fact[0] = 1;
  for (int i = 1; i <= N; i++)
    fact[i] = (i * fact[i - 1]) % mod;

  if (k == 0) {
    cout << (m * modexp(m - 1, n - 1)) % mod;
    return 0;
  }

  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ans = (ans +
           (choose(n - 1, i) * ((m * modexp(m - 1, n - 1 - i)) % mod)) % mod) %
          mod;
  }

  cout << ans << "\\n";
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define mod 998244353
using ll = long long;

ll fact[200002];
ll inv[200002];
ll invfact[200002];

ll modpow(ll n, ll m = mod - 2) {
  ll r = 1;
  while (m)
    r = r * ((m % 2) ? n : 1) % mod, n = n * n % mod, m >>= 1;
  return r;
}

void calc_fact_invfact() {
  if (fact[0] == 0) {
    fact[0] = inv[1] = invfact[0] = 1;
    for (int i = 2; i < 200002; i++)
      inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    for (int i = 1; i < 200002; i++)
      fact[i] = fact[i - 1] * i % mod,
      invfact[i] = invfact[i - 1] * inv[i] % mod;
  }
}
int nCk(int n, int k) {
  if (n < k || k < 0)
    return 0;
  else
    return fact[n] * invfact[k] % mod * invfact[n - k] % mod;
}

int main() {

  ll n, m, k;
  cin >> n >> m >> k;

  ll ans = 0;

  calc_fact_invfact();
  for (int i = 0; i <= k; i++)
    ans += nCk(n - 1, i) * (m % mod) % mod * modpow(m - 1, n - 1 - i) % mod;

  cout << ans % mod << endl;
}

Error: TLE

Fixed submission:

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

#define mod 998244353
using ll = long long;

ll fact[200002];
ll invfact[200002];

//(n^m)%mod
ll modpow(ll n, ll m, ll mod_pow) {
  mod_pow = mod;
  ll ans = 1;
  for (ll i = 0; i < m; i++) {
    ans = ans * n % mod_pow;
  }
  return ans;
}

void calc_fact_invfact() {
  fact[0] = 1;
  for (int i = 1; i < 200002; i++)
    fact[i] = fact[i - 1] * i % mod;

  invfact[200002 - 1] = modpow(fact[200002 - 1], mod - 2, mod);
  for (int i = 200002 - 2; i >= 0; i--)
    invfact[i] = invfact[i + 1] * (i + 1) % mod;
  // for(int i=2;i<200002;i++) invfact[i]=mod-invfact[mod%i]*(mod/i)%mod;
}

int nCk(int n, int k) {
  if (n < k || k < 0)
    return 0;
  else
    return fact[n] * invfact[k] * invfact[n - k] % mod;
}

int main() {

  int n, m, k;
  cin >> n >> m >> k;

  int ans = 0;

  calc_fact_invfact();
  for (int i = 0; i <= k; i++) {
    ans += nCk(n - 1, i) * m * modpow(m - 1, n - 1 - i, mod) % mod;
  }

  cout << fact[99] << " " << invfact[0] << " " << invfact[99] << endl;
  cout << ans % mod << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

static const llint MAX = 200005LL;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 998244353LL;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (llint 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;
  }
}

llint COM(llint n, llint k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

llint modpow(llint a, llint b) {
  if (b == 0)
    return 1LL;
  if (b % 2 == 0) {
    llint t = modpow(a, b / 2);
    return t * t % MOD;
  }
  return a * modpow(a, b - 1) % MOD;
}

int main(void) {
  llint n, m, k;
  cin >> n >> m >> k;

  COMinit();

  llint ans = 0, anstmp;
  for (llint ik = 0; ik <= k; ik++) {
    anstmp = COM(n - 1, ik);
    anstmp *= m;
    anstmp %= MOD;
    anstmp *= modpow(m - 1, n - 1 - ik);
    anstmp %= MOD;
    ans += anstmp;
    ans %= MOD;
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

static const llint MAX = 200005LL;
// static const int NIL = -1;
// static const ll INF = 1<<21;
static const llint MOD = 998244353LL;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (llint 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;
  }
}

llint COM(llint n, llint k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

llint modpow(llint a, llint b) {
  llint ans = 1;
  for (llint i = 0; i < b; i++) {
    ans *= a;
    ans %= MOD;
  }
  return ans;
}

int main(void) {
  llint n, m, k;
  cin >> n >> m >> k;

  COMinit();

  llint ans = 0, anstmp;
  for (llint ik = 0; ik <= k; ik++) {
    anstmp = COM(n - 1, ik);
    anstmp *= m;
    anstmp %= MOD;
    anstmp *= modpow(m - 1, n - 1 - ik);
    anstmp %= MOD;
    ans += anstmp;
    ans %= MOD;
  }

  cout << ans << endl;
  return 0;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 10000000000
#define MOD 998244353
using ll = long long;
using Graph = vector<vector<int>>;

// 二項係数を求める。
const int MAX = 210000;

ll 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;
  }
}

// 二項係数計算
ll 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;
}

ll f(int n, int m) {
  ll tmp = m;
  rep(i, n - 1) {
    tmp *= (m - 1);
    tmp %= MOD;
  }
  return tmp;
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  COMinit();

  ll res = 0, F = 0;
  for (int i = min(N - 1, K); i >= 0; i--) {
    // iは色ダブりの接点の数。
    if (i == min(N - 1, K))
      F = f(N - i, M);
    else
      F *= (M - 1);
    F %= MOD;
    ll tmp = COM(N - 1, i);
    tmp *= F;
    res += tmp;
    res %= MOD;
  }
  cout << res << endl;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 10000000000
#define MOD 998244353
using ll = long long;
using Graph = vector<vector<int>>;

// 二項係数を求める。
const int MAX = 210000;

ll 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;
  }
}

// 二項係数計算
ll 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;
}

ll f(int n, int m) {
  ll tmp = m;
  rep(i, n - 1) {
    tmp *= (m - 1);
    tmp %= MOD;
  }
  return tmp;
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  COMinit();

  ll res = 0, F = 0;
  for (int i = min(N - 1, K); i >= 0; i--) {
    // iは色ダブりの接点の数。
    if (!F)
      F = f(N - i, M);
    else
      F *= (M - 1);
    F %= MOD;
    ll tmp = COM(N - 1, i) % MOD;
    tmp *= F;
    res += tmp;
    res %= MOD;
  }
  cout << res << endl;
}
Problem p02685 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll mod = 998244353;
vector<ll> fact;
vector<ll> ifact;

ll mod_pow(ll x, ll n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}

void mod_ifact(ll n) {
  fact.resize(n + 1);
  fill(fact.begin(), fact.end(), 1);
  ifact.resize(n + 1);
  fill(ifact.begin(), ifact.end(), 0);
  for (ll i = 1; i < n + 1; i++)
    fact[i] = fact[i - 1] * i % mod;
  ifact[n] = mod_pow(fact[n], mod - 2);
  for (ll i = n; i > 0; i--)
    ifact[i - 1] = ifact[i] * i % mod;
}

ll mod_comb(ll n, ll k) {
  if (k < 0 || k > n)
    return 0;
  return fact[n] * ifact[k] % mod * ifact[n - k] % mod;
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  mod_ifact(n);
  ll ans = 0;
  rep(i, k + 1) ans =
      (ans + mod_comb(n - 1, i) * m % mod * mod_pow(m - 1, n - 1 - i)) % mod;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ll mod = 998244353;
  int n, m, k;
  cin >> n >> m >> k;
  vector<ll> dp(k + 1, 0);
  dp[0] = m;
  rep(i, n - 1) for (int j = k; j >= 0; j--) {
    if (j < k)
      dp[j + 1] = (dp[j + 1] + dp[j]) % mod;
    dp[j] = dp[j] * (m - 1) % mod;
  }
  ll ans = 0;
  rep(i, k + 1) ans = (ans + dp[i]) % mod;
  cout << ans << endl;
  return 0;
}