Problem p02632 (C++)

Buggy submission (status: Runtime Error):

// Problem : F - Strivore
// Contest : AtCoder - AtCoder Beginner Contest 171
// URL : https://atcoder.jp/contests/abc171/tasks/abc171_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define die return 0;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int MAX = 1e6;

ll modulo(ll a, ll b, ll n) {
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y) % n; // multiplying with base
    }
    y = (y * y) % n; // squaring the base
    b /= 2;
  }
  return x % n;
}

vector<int> fact(MAX + 1), finv(MAX + 1);

void pre() {
  fact[0] = 1, finv[0] = 1;
  for (int i = 1; i <= MAX; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  for (int i = 1; i <= MAX; i++) {
    finv[i] = modulo(fact[i], MOD - 2, MOD);
  }
}

int nCr(int n, int r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  if (r == 0 || r == n)
    return 1;
  return ((((fact[n] * finv[r]) % MOD) * finv[n - r]) % MOD);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int testcases;
  testcases = 1;
  // cin>>testcases;

  while (testcases--) {
    pre();
    int k;
    string str;
    cin >> k;
    cin >> str;
    int ans = 0;
    int len = str.length();
    int fin = len + k;
    for (int i = len; i <= fin; i++) {
      int last = fin - i;
      int temp = modulo(26, last, MOD);
      temp = (temp * modulo(25, fin - last - len, MOD)) % MOD;
      temp = (temp * nCr(i - 1, len - 1)) % MOD;
      ans = (ans + temp) % MOD;
    }

    cout << ans << '\n';
  }

  return 0;
}


Fixed submission:

// Problem : F - Strivore
// Contest : AtCoder - AtCoder Beginner Contest 171
// URL : https://atcoder.jp/contests/abc171/tasks/abc171_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define db1(x) cout << #x << "=" << x << endl
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \
       << endl
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i >= b; i--)
#define die return 0;

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int MAX = 2e6 + 7;

ll modulo(ll a, ll b, ll n) {
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y) % n; // multiplying with base
    }
    y = (y * y) % n; // squaring the base
    b /= 2;
  }
  return x % n;
}

vector<int> fact(MAX + 1), finv(MAX + 1);

void pre() {
  fact[0] = 1, finv[0] = 1;
  for (int i = 1; i <= MAX; i++) {
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  for (int i = 1; i <= MAX; i++) {
    finv[i] = modulo(fact[i], MOD - 2, MOD);
  }
}

int nCr(int n, int r) {
  if (n < r)
    return 0;
  if (n < 0 || r < 0)
    return 0;
  if (r == 0 || r == n)
    return 1;
  return ((((fact[n] * finv[r]) % MOD) * finv[n - r]) % MOD);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int testcases;
  testcases = 1;
  // cin>>testcases;

  while (testcases--) {
    pre();
    int k;
    string str;
    cin >> k;
    cin >> str;
    int ans = 0;
    int len = str.length();
    int fin = len + k;
    for (int i = len; i <= fin; i++) {
      int last = fin - i;
      int temp = modulo(26, last, MOD);
      temp = (temp * modulo(25, fin - last - len, MOD)) % MOD;
      temp = (temp * nCr(i - 1, len - 1)) % MOD;
      ans = (ans + temp) % MOD;
    }

    cout << ans << '\n';
  }

  return 0;
}