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

#include <bits/stdc++.h>
using namespace std;

int main() {

  vector<int> mod(224);
  for (int i = 0; i < 224; i++) {
    if (i == 0) {
      mod.at(i) = 1;
    } else {
      mod.at(i) = 10 * mod.at(i - 1) % 2019;
    }
  }

  // sort(mod.begin(),mod.end());
  /*for(int i=0; i<224; i++){
    cout << "i" << i << ":" << mod.at(i) <<endl;
  }*/

  string S;
  cin >> S;

  long long int N = (long int)(S.length()); // Nketa
  // cout << len << endl;

  vector<long int> s(N);
  // cout << (s.size()) << endl;

  for (long long int i = 0; i < N; i++) {
    long int tmp = S.at(i) - '0';
    s.at(i) = tmp;
    // cout << s.at(i) ;
  }

  vector<int> t(N + 1);
  for (long long int i = 0; i < N + 1; i++) {
    if (i == 0) {
      t.at(i) = 0;
    } else if (i == 1) {
      t.at(i) = s.at(N - i);
    } else {
      int tmp;
      tmp = (i - 1) % 224;
      t.at(i) = (t.at(i - 1) + s.at(N - i) * mod.at(tmp)) % 2019;
    }
  }

  long long int count = 0;
  long long int sum = 1;

  sort(t.begin(), t.end());
  for (long long int i = 0; i < N; i++) {
    if (t.at(i) == t.at(i + 1)) {
      sum++;
      if (i == N - 1) {
        count += sum * (sum - 1) / 2;
      }
    } else {
      count += sum * (sum - 1) / 2;
      sum = 1;
    }
  }
  cout << count << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {

  vector<int> mod(2019);
  for (int i = 0; i < 2019; i++) {
    if (i == 0) {
      mod.at(i) = 1;
    } else {
      mod.at(i) = 10 * mod.at(i - 1) % 2019;
    }
  }

  string S;
  cin >> S;

  // S = "20192019";

  int N = S.size(); // Nketa
  // cout << len << endl;

  vector<int> s(N);
  // cout << (s.size()) << endl;

  for (int i = 0; i < N; i++) {
    int tmp = S.at(i) - '0';
    s.at(i) = tmp;
    // cout << s.at(i) ;
  }

  // reverse(s.begin(),s.end());

  vector<long int> t(N + 1);
  for (int i = 0; i < N + 1; i++) {
    if (i == 0) {
      // T.at(i)=0;
      t.at(i) = 0;
      // cout << t.at(i) << endl;
    } else if (i == 1) {
      // T.at(i)=0;
      t.at(i) = s.at(N - i);
      // cout << t.at(i) << endl;
    } else {
      // T.at(i) = (T.at(i-1) + s.at(N-i)*pow(10,i-1));
      t.at(i) = (t.at(i - 1) + s.at(N - i) * mod.at(i - 1)) % 2019;

      // cout << "s" << s.at(N-i) << "t" << t.at(i) << endl;
    }
  }

  int count = 0;
  int sum = 0;

  reverse(t.begin(), t.end());

  for (int i = 0; i < N - 2; i++) {
    for (int j = 4; i + j < N + 1; j++) {
      if (t.at(i) == t.at(i + j)) {
        sum++;
        if (i + j == N) {
          count += sum;
          sum = 0;
        }
      } else {
        if (i + j == N) {
          count += sum;
          sum = 0;
        } else {
          continue;
        }
      }
      // cout << "i" << i << "j" << j << sum << count << endl;
    }
  }

  /*sort(t.begin(),t.end());
  cout << "next"  << endl;

  int count=0;
  int sum = 1;
  for(int i=0; i<N-1; i++){
    cout << t.at(i) << endl;
    if(t.at(i)==t.at(i+1)){
      sum++;
    }
    else{
      if(sum==1){
        continue;
      }
      else{ // sum!=1
      count += sum*(sum-1)/2;
      cout << "i" << i  << sum << endl;
      sum = 1;
      }
    }
  }*/
  cout << count << endl;
}