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

#pragma GCC optimize("Ofast", 3, "inline")
#include <bits/stdc++.h>
using namespace std;
#define fir first
#define sec second
#define m_p make_pair
#define y1 ygftgfgcdtfgxffgx
#define y2 yfdsesgvtyghftfvv
#define x1 xvyr6cf6fgcfgf676
#define x2 xcr6rfc5r66y6r6fr
#define up_bound upper_bound
#define low_bound lower_bound
#define next_per next_permutation
#define pb push_back
#define i_to_s to_string
typedef priority_queue<int> p_queue;
typedef priority_queue<int, vector<int>, greater<int>> min_p_queue;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int mon[20] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll gcd(ll x, ll y) { return ((y == 0) ? x : gcd(y, x % y)); }
// ll a,b,n;
// string s;
// ll ans;
// inline ll make(int i,int j){
//	int num=0;
//	for(int k=i;k<=j;k++)num=num*10+(s[k]-'0');
//	return num;
// }
// int main(){
//	ios_base::sync_with_stdio(false);
//	cin.tie(0);
////	freopen(".in","r",stdin);
////	freopen(".out","w",stdout);
//	cin>>s;n=s.size();
//	for(int i=0;i<n;i++){
//		int j=i+3;
//		a=b=0;
//		a=make(i,j);
//		j++;
////		cout<<i<<' '<<j<<':';
//		while(j<n){
////			cout<<a<<' ';
//			if(a%2019==0){
//				ans++;//cout<<i<<' '<<j<<'\\n';//	cout<<a<<'
//';
////				cout<<"++ ";
//				a=0;
//				j+=4;
//				a=(s[j]-'0');
//				if(j>=n){
//					a=2020;
//					break;
//				}
//				continue;
//			}
//			else {
//				a%=2019;
//			}
//			a=a*10+(s[j]-'0');
//			j++;
//		}
//		if((j==n)&&a%2019==0){
//			ans++;//cout<<i<<' '<<j<<'\\n';//	cout<<a<<' ';
////			cout<<"++ ";
////			a=0;
////			j+=4;
////			continue;
//		}
////		cout<<'\\n';
////		cout<<ans;
//	}
//	cout<<ans;
//	return 0;
//}

int a[200010], ans = 0;
string s;
int main() {
  cin >> s;
  a[0] = 1;
  for (int i = s.size() - 1, j = 1, k = 0; i >= 0; i--, j = j * 10 % 2019) {
    k = (k + (s[i] - '0') * j % 2019) % 2019;
    ans += a[k];
    a[k]++;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast", 3, "inline")
#include <bits/stdc++.h>
using namespace std;
#define fir first
#define sec second
#define m_p make_pair
#define y1 ygftgfgcdtfgxffgx
#define y2 yfdsesgvtyghftfvv
#define x1 xvyr6cf6fgcfgf676
#define x2 xcr6rfc5r66y6r6fr
#define up_bound upper_bound
#define low_bound lower_bound
#define next_per next_permutation
#define pb push_back
#define i_to_s to_string
typedef priority_queue<int> p_queue;
typedef priority_queue<int, vector<int>, greater<int>> min_p_queue;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int mon[20] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
ll gcd(ll x, ll y) { return ((y == 0) ? x : gcd(y, x % y)); }
ll a, b, n;
string s;
ll ans;
inline ll make(int i, int j) {
  int num = 0;
  for (int k = i; k <= j; k++)
    num = num * 10 + (s[k] - '0');
  return num;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  //	freopen(".in","r",stdin);
  //	freopen(".out","w",stdout);
  cin >> s;
  n = s.size();
  for (int i = 0; i < n; i++) {
    int j = i + 3;
    a = b = 0;
    a = make(i, j);
    j++;
    //		cout<<i<<' '<<j<<':';
    while (j < n) {
      //			cout<<a<<' ';
      if (a % 2019 == 0) {
        ans++; // cout<<i<<' '<<j<<'\\n';//	cout<<a<<' ';
        //				cout<<"++ ";
        a = 0;
        j += 4;
        a = (s[j] - '0');
        if (j >= n) {
          a = 2020;
          break;
        }
        continue;
      } else {
        a %= 2019;
      }
      a = a * 10 + (s[j] - '0');
      j++;
    }
    if ((j == n) && a % 2019 == 0) {
      ans++; // cout<<i<<' '<<j<<'\\n';//	cout<<a<<' ';
      //			cout<<"++ ";
      //			a=0;
      //			j+=4;
      //			continue;
    }
    //		cout<<'\\n';
    //		cout<<ans;
  }
  cout << ans;
  return 0;
}