Problem p02632 (C++) — buggy submission, verdict: 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>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define RI(i, n) FOR(i, 1, (n))
#define REP(i, n) FOR(i, 0, (n)-1)
#define mini(a, b) a = min(a, b)
#define maxi(a, b) a = max(a, b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int)w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 3e3 + 5;
const ll MOD = 1e9 + 7;

ll dp[maxN][maxN];

ll pow2(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll d = pow2(a, b / 2);
  d = d * d % MOD;
  if (b % 2)
    return d * a % MOD;
  return d;
}

const ll MAX = 2e6;
ll fact[MAX + 5], invFact[MAX + 5];
void facts() {
  fact[0] = invFact[0] = 1;
  RI(i, MAX) {
    fact[i] = fact[i - 1] * i % MOD;
    invFact[i] = pow2(fact[i], MOD - 2);
  }
}

ll choose(ll n, ll k) {
  return (fact[n] * invFact[k] % MOD) * invFact[n - k] % MOD;
}

// sprawdz MODULO!
int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int m = sz(s);
  facts();
  ll ans = 0;
  FOR(i, m, n + m) {
    ll tmp = choose(n + m, i) * pow2(25, n + m - i);
    ans = (ans + tmp) % MOD;
  }
  cout << ans << "\\n";
  return 0;
}


Error: WA

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>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define RI(i, n) FOR(i, 1, (n))
#define REP(i, n) FOR(i, 0, (n)-1)
#define mini(a, b) a = min(a, b)
#define maxi(a, b) a = max(a, b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int)w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 3e3 + 5;
const ll MOD = 1e9 + 7;

ll dp[maxN][maxN];

ll pow2(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll d = pow2(a, b / 2);
  d = d * d % MOD;
  if (b % 2)
    return d * a % MOD;
  return d;
}

ll solve(int len, int pos) {
  if (len == 0) {
    return pos == 0;
  }
  if (pos == 0) {
    return pow2(26, len);
  }
  if (dp[len][pos] != -1)
    return dp[len][pos];
  ll ans = solve(len - 1, pos) * 25 + solve(len - 1, pos - 1);
  dp[len][pos] = ans % MOD;
  return ans % MOD;
}

// sprawdz MODULO!
int main() {
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  REP(i, 1000) {
    REP(j, 1000) { dp[i][j] = -1; }
  }
  // cout << solve(4, 3) << endl;
  cout << solve(n + sz(s), sz(s));
  return 0;
}