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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;
const dd PI = 3.1415926435;

class UnionFind {
public:
  UnionFind(int N) : par(N) {
    for (ll i = 0; i < N; i++) {
      par[i] = i;
    }
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

private:
  vector<int> par;
};

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

ll fact(ll n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return 1;
  }
  return n * fact(n - 1) % MOD;
}

ll pow_fast(ll n, ll k) {
  if (k == 0) {
    return 1;
  }
  if (k == 1) {
    return n;
  }

  if (k % 2 == 0) {
    ll tmp = pow_fast(n, k / 2LL);
    return tmp * tmp % MOD;
  } else {
    ll tmp = pow_fast(n, k / 2LL);
    tmp *= tmp;
    tmp %= MOD;
    return tmp * n % MOD;
  }
}

map<ll, ll> sosuu(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i] += 1;
      n /= i;
    }
  }
  if (n != 1) {
    res[n] += 1;
  }
  return res;
}

struct Dish {
  ll time;
  ll taste;
};

static ll FACTORIAL[10000010] = {};
ll nCk(ll n, ll k) {
  if (FACTORIAL[1] == 0) {
    FACTORIAL[0] = 1;
    FACTORIAL[1] = 1;
    for (ll i = 2; i <= 10000000; i++) {
      FACTORIAL[i] = FACTORIAL[i - 1] * i;
      FACTORIAL[i] %= MOD;
    }
  }
  ll N_fact = FACTORIAL[n];
  ll K_fact = pow_fast(FACTORIAL[k], MOD - 2);
  ll N_K_fact = pow_fast(FACTORIAL[n - k], MOD - 2);
  ll ret = N_fact;
  ret %= MOD;
  ret *= K_fact;
  ret %= MOD;
  ret *= N_K_fact;
  ret %= MOD;
  return ret;
}

void solve(string s, ll K) {
  ll sum = 0;
  for (ll i = s.size(); i <= s.size() + K; i++) {
    // 25
    ll cnt = pow_fast(25, i - s.size()) % MOD;
    cnt *= nCk(i - 1, s.size() - 1);
    cnt %= MOD;

    // 26
    cnt *= pow_fast(26, s.size() + K - i);
    cnt %= MOD;

    sum += cnt;
    sum %= MOD;
  }
  cout << sum << endl;
  return;
}

int main() {
  ll K;
  string S;
  cin >> K >> S;
  solve(S, K);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;
const dd PI = 3.1415926435;

class UnionFind {
public:
  UnionFind(int N) : par(N) {
    for (ll i = 0; i < N; i++) {
      par[i] = i;
    }
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return;
    par[rx] = ry;
  }

  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }

private:
  vector<int> par;
};

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}

ll fact(ll n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return 1;
  }
  return n * fact(n - 1) % MOD;
}

ll pow_fast(ll n, ll k) {
  if (k == 0) {
    return 1;
  }
  if (k == 1) {
    return n;
  }

  if (k % 2 == 0) {
    ll tmp = pow_fast(n, k / 2LL);
    return tmp * tmp % MOD;
  } else {
    ll tmp = pow_fast(n, k / 2LL);
    tmp *= tmp;
    tmp %= MOD;
    return tmp * n % MOD;
  }
}

map<ll, ll> sosuu(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      res[i] += 1;
      n /= i;
    }
  }
  if (n != 1) {
    res[n] += 1;
  }
  return res;
}

struct Dish {
  ll time;
  ll taste;
};

const ll MAX_COMB = 10;

static ll FACTORIAL[1000010] = {};
ll nCk(ll n, ll k) {
  if (FACTORIAL[1] == 0) {
    FACTORIAL[0] = 1;
    FACTORIAL[1] = 1;
    for (ll i = 2; i <= 1000000; i++) {
      FACTORIAL[i] = FACTORIAL[i - 1] * i;
      FACTORIAL[i] %= MOD;
    }
  }
  ll N_fact = FACTORIAL[n];
  ll K_fact = pow_fast(FACTORIAL[k], MOD - 2);
  ll N_K_fact = pow_fast(FACTORIAL[n - k], MOD - 2);
  ll ret = N_fact;
  ret %= MOD;
  ret *= K_fact;
  ret %= MOD;
  ret *= N_K_fact;
  ret %= MOD;
  return ret;
}

void solve(string s, ll K) {
  ll sum = 0;
  for (ll i = s.size(); i <= s.size() + K; i++) {
    // 25
    ll cnt = pow_fast(25, i - s.size()) % MOD;
    cnt *= nCk(i - 1, s.size() - 1);
    cnt %= MOD;

    // 26
    cnt *= pow_fast(26, s.size() + K - i);
    cnt %= MOD;

    sum += cnt;
    sum %= MOD;
  }
  cout << sum << endl;
  return;
}

int main() {
  ll K;
  string S;
  cin >> K >> S;
  solve(S, K);
}