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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define allR(v) v.rbegin(), v.rend()
#define PRINT(v)                                                               \\
  for (auto x : (v))                                                           \\
    cout << x << " ";                                                          \\
  cout << endl;
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
typedef pair<int, int> Pi;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
const int inf = 1001001001;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline T powerM(T a, T b) {
  if (b == 0)
    return 1;
  T tmp = powerM(a, b / 2);
  if (b % 2 == 0)
    return tmp * tmp % MOD;
  else
    return tmp * tmp % MOD * a % MOD;
}
template <class T> inline T power(T a, T b, T m = numeric_limits<T>::max()) {
  if (b == 0)
    return 1;
  T tmp = power(a, b / 2, m);
  if (b % 2 == 0)
    return tmp * tmp % m;
  else
    return tmp * tmp % m * a % m;
}
template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
// ax+by=gcd(a,b)を解く
template <class T> inline T extgcd(T a, T b, T &x, T &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
void hey() { cout << "hey" << endl; }

template <class T> struct edge {
  int to;
  T cost;
};

// 二項係数nCk mod pの計算-------------------------------------
const int MAX = 2010000;
// 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;
}

// 計算例:O(1)
// cout << COM(100000, 50000) << endl;

void solve() {
  int k;
  cin >> k;
  string s;
  cin >> s;
  int n = s.size();
  int m = n + k;

  ll res = 1;
  ll beki25 = 1;
  for (int i = 1; i <= k; i++) {
    beki25 *= 25;
    beki25 %= MOD;
    ll tmp = COM(m, i) * beki25 % MOD;
    res += tmp;
    res %= MOD;
  }
  cout << res << endl;

  // the answer is

  // vector<vector<ll>> dp(m+1, vector<ll>(n+1, 0));
  // dp[0][0] = 1;
  // // i文字目までで s の文字を j 個 使ってます
  // for (int i=0; i<m; i++){
  //   for (int j=0; j<=n; j++){
  //     if (j < n){
  //       dp[i+1][j] += dp[i][j] * 25 % MOD;
  //       dp[i+1][j+1] += dp[i][j];
  //       dp[i+1][j] %= MOD;
  //       dp[i+1][j+1] %= MOD;
  //     }else{
  //       dp[i+1][j] += dp[i][j] * 26 % MOD;
  //       dp[i+1][j] %= MOD;
  //     }
  //     // dp[i+1][j] = (dp[i+1][j] + dp[i][j] * 25 % MOD) % MOD;
  //     // if (j < k) dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j]) % MOD;
  //     // else dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % MOD;
  //   }
  // }
  // ll res = dp[m][n];
  // cout <<res <<endl;
}

int main() {
  COMinit();
  solve();
  // solve2();
}

/*

aaba

aaaba
aaaaba
aaabaa

aabaa
aaabaa
aabaaa


aaab

aaaab
aaaaab
aaaaba


aaaba
aaaaba
aaabaa




 */

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define allR(v) v.rbegin(), v.rend()
#define PRINT(v)                                                               \\
  for (auto x : (v))                                                           \\
    cout << x << " ";                                                          \\
  cout << endl;
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
typedef pair<int, int> Pi;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
const int inf = 1001001001;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline T powerM(T a, T b) {
  if (b == 0)
    return 1;
  T tmp = powerM(a, b / 2);
  if (b % 2 == 0)
    return tmp * tmp % MOD;
  else
    return tmp * tmp % MOD * a % MOD;
}
template <class T> inline T power(T a, T b, T m = numeric_limits<T>::max()) {
  if (b == 0)
    return 1;
  T tmp = power(a, b / 2, m);
  if (b % 2 == 0)
    return tmp * tmp % m;
  else
    return tmp * tmp % m * a % m;
}
template <class T> inline T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
// ax+by=gcd(a,b)を解く
template <class T> inline T extgcd(T a, T b, T &x, T &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  T d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
void hey() { cout << "hey" << endl; }

template <class T> struct edge {
  int to;
  T cost;
};

// 二項係数nCk mod pの計算-------------------------------------
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;
}

// 計算例:O(1)
// cout << COM(100000, 50000) << endl;

void solve() {
  int k;
  cin >> k;
  string s;
  cin >> s;
  int n = s.size();
  int m = n + k;

  ll res = 1;
  ll beki25 = 1;
  for (int i = 1; i <= m - n; i++) {
    beki25 *= 25;
    beki25 %= MOD;
    ll tmp = COM(m, i) * beki25 % MOD;
    res += tmp;
    res %= MOD;
  }
  cout << res << endl;

  // the answer is

  // vector<vector<ll>> dp(m+1, vector<ll>(n+1, 0));
  // dp[0][0] = 1;
  // // i文字目までで s の文字を j 個 使ってます
  // for (int i=0; i<m; i++){
  //   for (int j=0; j<=n; j++){
  //     if (j < n){
  //       dp[i+1][j] += dp[i][j] * 25 % MOD;
  //       dp[i+1][j+1] += dp[i][j];
  //       dp[i+1][j] %= MOD;
  //       dp[i+1][j+1] %= MOD;
  //     }else{
  //       dp[i+1][j] += dp[i][j] * 26 % MOD;
  //       dp[i+1][j] %= MOD;
  //     }
  //     // dp[i+1][j] = (dp[i+1][j] + dp[i][j] * 25 % MOD) % MOD;
  //     // if (j < k) dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j]) % MOD;
  //     // else dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % MOD;
  //   }
  // }
  // ll res = dp[m][n];
  // cout <<res <<endl;
}

int main() {
  COMinit();
  solve();
  // solve2();
}

/*

aaba

aaaba
aaaaba
aaabaa

aabaa
aaabaa
aabaaa


aaab

aaaab
aaaaab
aaaaba


aaaba
aaaaba
aaabaa




 */