Problem p02702 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
void clearCharArray(char *c, int length) {
  for (int i = 0; i < length; i++) {
    c[i] = 0;
  }
}
int main() {
  string s;
  cin >> s;
  int length = s.length();
  int result = 0;
  const char *p_c = s.c_str();
  int mod = 2019;
  vector<int> givenNumbers(length);
  vector<int> totalNumberModRemain(length);
  // cout << length;
  // cout << "A"<<endl;
  for (int i = 0; i < length; i++) {
    int tmp = (int)(p_c[length - i - 1] - '0');
    // cout << "i:" <<i<<" ";
    // cout << tmp<<endl;
    givenNumbers[i] = tmp;
  }
// cout << "B"<<endl;
#if 0
    for(int i = (givenNumbers.size()-1);i>=0;i--){
        cout << givenNumbers[i];
    }
    cout << endl;
#endif
  long order = 1;
  for (int i = 0; i < length; i++) {
    int tmp = 0;
    tmp += (givenNumbers[i] % mod);
    if (i != 0) {
      order *= 10;
      order %= mod;
      tmp *= order;
      tmp %= mod;
      tmp += totalNumberModRemain[i - 1];
      tmp %= mod;
    }
    totalNumberModRemain[i] = (tmp);
  }
  // cout << "C"<<endl;
  vector<int> sum(2019);
  for (int i = 0; i < totalNumberModRemain.size(); i++) {
    sum[totalNumberModRemain[i]]++;
  }
  // 余が0の場合は、1プラス(1桁目から数えるため。)
  sum[0]++;
#if 0
    for(int i = 0; i < sum.size(); i++){
        if(sum[i]!=0){
            cout <<"i:"<<i<<" "<<sum[i]<<endl;
        }
    }
#endif
  // nCrで二つの選び方を数え上げる。
  for (int i = 0; i < sum.size(); i++) {
    if (sum[i] == 2) {
      result++;
    } else if (sum[i] > 2) {
      result += (sum[i] * (sum[i] - 1)) / 2;
    } else {
      // do nothing
    }
  }
  cout << result << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void clearCharArray(char *c, int length) {
  for (int i = 0; i < length; i++) {
    c[i] = 0;
  }
}
int main() {
  string s;
  cin >> s;
  int length = s.length();
  int result = 0;
  const char *p_c = s.c_str();
  int mod = 2019;
  vector<int> givenNumbers(length);
  vector<int> totalNumberModRemain(length);
  // cout << length;
  for (int i = 0; i < length; i++) {
    int tmp = (int)(p_c[length - i - 1] - '0');
    // cout << "i:" <<i<<" ";
    // cout << tmp<<endl;
    givenNumbers[i] = tmp;
  }
#if 0
    for(int i = (givenNumbers.size()-1);i>=0;i--){
        cout << givenNumbers[i];
    }
    cout << endl;
#endif
  for (int i = 0; i < length; i++) {
    int tmp = 0;
    tmp += (givenNumbers[i] % mod);
    for (int j = 0; j < i; j++) {
      tmp *= 10;
      tmp %= mod;
    }
    if (i != 0) {
      tmp += totalNumberModRemain[i - 1];
      tmp %= mod;
    }
    totalNumberModRemain[i] = (tmp);
  }
#if 0
    for(int i = 0; i<totalNumberModRemain.size();i++){
        cout << totalNumberModRemain[i]<<endl;
    }
#endif
  for (int i = 0; i < length; i++) {
    // cout << "i:"<<i<<endl;
    for (int j = i + 3; j < length; j++) {
      // cout << "j:"<<j<<endl;
      // if((j-i)>=3){
      long tmp = 0;
      if (i == 0) {
        tmp = totalNumberModRemain[j];
      } else {
        tmp = totalNumberModRemain[j] - totalNumberModRemain[i - 1];
      }
      // cout << "i:"<<i<<" j:"<<j<<" ";
      // cout <<"tmp "<< tmp << endl;
      if ((tmp == 0) || (tmp % mod == 0)) {
        result++;
      }
      //}
    }
  }
  cout << result << endl;
  return 0;
}