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


// #pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd

#define eps 1e-9
#define bs 1000000007
#define bsize 256

using namespace std;

const int INF = 1e9;
const int N = 3000002;

string st;
int tests;
int cur_size;
int whr[N];
int done[N];
vector<int> memo[N];

int DIF;

long long Pw[N];

long long fact[N], invf[N];

long long pw(long long a, long long b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return a * pw(a, b - 1) % bs;
  return pw(a * a % bs, b / 2);
}

long long inv(long long x) { return pw(x, bs - 2); }

long long C(long long n, long long m) {
  long long res = fact[n];
  res *= invf[m];
  res %= bs;
  res *= invf[n - m];
  return res % bs;
}
long long count_ways(int n, int m) {
  if (n < m)
    return 0;
  long long picks = C(n, m);
  return picks * Pw[n - m] % bs;
}

void run_solver(int sz) {
  if (done[sz])
    return;
  done[sz] = 1;
  long long total = 0;

  ++DIF;

  vector<int> v;
  v.push_back(0);
  for (int len = 1; len < N; len++) {
    total = total * 26 % bs;
    // if (len<10)
    // cout << count_ways(len - 1, sz - 1) << endl;

    total += count_ways(len - 1, sz - 1);
    total %= bs;
    v.push_back(total);
  }

  memo[DIF] = v;
  whr[sz] = DIF;
}

int main() {
  // freopen("fabro.in","r",stdin);
  // freopen("fabro.out","w",stdout);
  // freopen("F:/in.txt", "r", stdin);
  // freopen("F:/output.txt", "w", stdout);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif

  ios_base::sync_with_stdio(0);
  // cin.tie(0);

  Pw[0] = 1;
  for (int i = 1; i < N; i++) {
    Pw[i] = Pw[i - 1] * 25 % bs;
  }

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

  for (int i = 0; i < N; i++) {
    invf[i] = inv(fact[i]);
  }

  tests = 1;
  int len;
  cin >> len;
  cin >> st;
  len += st.length();
  run_solver(st.size());

  cur_size = st.size();

  for (; tests; --tests) {
    int tp;
    tp = 2;
    if (tp == 1) {
      cin >> st;
      run_solver(st.size());
      cur_size = st.size();
    } else {
      int id = whr[cur_size];
      cout << memo[id][len] << endl;
    }
  }

  cin.get();
  cin.get();
  return 0;
}


Error: 0

Fixed submission:


// #pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd

#define eps 1e-9
#define bs 1000000007
#define bsize 256

using namespace std;

const int INF = 1e9;
const int N = 1000002;

string st;
int tests;
int cur_size;
int whr[N];
int done[N];
vector<int> memo[N];

int DIF;

long long Pw[N];

long long fact[N], invf[N];

long long pw(long long a, long long b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return a * pw(a, b - 1) % bs;
  return pw(a * a % bs, b / 2);
}

long long inv(long long x) { return pw(x, bs - 2); }

long long C(long long n, long long m) {
  long long res = fact[n];
  res *= invf[m];
  res %= bs;
  res *= invf[n - m];
  return res % bs;
}
long long count_ways(int n, int m) {
  if (n < m)
    return 0;
  long long picks = C(n, m);
  return picks * Pw[n - m] % bs;
}

void run_solver(int sz) {
  if (done[sz])
    return;
  done[sz] = 1;
  long long total = 0;

  ++DIF;

  vector<int> v;
  v.push_back(0);
  for (int len = 1; len < N; len++) {
    total = total * 26 % bs;
    // if (len<10)
    // cout << count_ways(len - 1, sz - 1) << endl;

    total += count_ways(len - 1, sz - 1);
    total %= bs;
    v.push_back(total);
  }

  memo[DIF] = v;
  whr[sz] = DIF;
}

int main() {
  // freopen("fabro.in","r",stdin);
  // freopen("fabro.out","w",stdout);
  // freopen("F:/in.txt", "r", stdin);
  // freopen("F:/output.txt", "w", stdout);

  ios_base::sync_with_stdio(0);
  // cin.tie(0);

  Pw[0] = 1;
  for (int i = 1; i < N; i++) {
    Pw[i] = Pw[i - 1] * 25 % bs;
  }

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

  for (int i = 0; i < N; i++) {
    invf[i] = inv(fact[i]);
  }

  tests = 1;
  int len;
  cin >> len;
  cin >> st;
  len += st.length();
  run_solver(st.size());

  cur_size = st.size();

  for (; tests; --tests) {
    int tp;
    tp = 2;
    if (tp == 1) {
      cin >> st;
      run_solver(st.size());
      cur_size = st.size();
    } else {
      int id = whr[cur_size];
      cout << memo[id][len] << endl;
    }
  }

  cin.get();
  cin.get();
  return 0;
}