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

#include <iostream>
// #include <iomanip>
#include <string>
#include <vector>
// #include <algorithm>
// #include <utility>
// #include <set>
#include <map>
// #include <queue>
// #include <deque>
// #include <bitset>
// #include <math.h>
using namespace std;
using ll = long long;
// using ld = long double ;
using vll = vector<ll>;
// using vvll = vector<vll> ;
// using vc = vector<char> ;
// using vvc = vector<vc> ;
// using vb = vector<bool> ;
// using vvb = vector<vb> ;
// using pll = pair<ll,ll> ;
// #define all(v) v.begin(),v.end()
// ll mod = 1000000007 ;
// long double pie = acos(-1) ;
// ll INF = 1000000000000 ;

// void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
// void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
// ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
// ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
// ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
// void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}
// void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}
// void fix_cout(){cout << fixed << setprecision(20) ;}

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vll sum(n + 1, 0);
  ll ten = 1;
  for (int i = 0; i < n; i++) {
    sum.at(n - 1 - i) = (sum.at(n - i) + (s.at(n - 1 - i) - '0') * ten) % 2019;
    ten = ten * 10 % 2019;
  }
  //	for(auto i:sum) cout << i << " " ; cout << endl ;
  map<ll, ll> m;
  for (int i = 0; i <= n; i++)
    m[sum.at(i)]++;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans += m[sum.at(i + 1)] - 1;
    m[sum.at(i + 1)]--;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
// #include <iomanip>
#include <string>
#include <vector>
// #include <algorithm>
// #include <utility>
// #include <set>
// #include <map>
// #include <queue>
// #include <deque>
// #include <bitset>
// #include <math.h>
using namespace std;
using ll = long long;
// using ld = long double ;
using vll = vector<ll>;
using vvll = vector<vll>;
// using vc = vector<char> ;
// using vvc = vector<vc> ;
// using vb = vector<bool> ;
// using vvb = vector<vb> ;
// using pll = pair<ll,ll> ;
// #define all(v) v.begin(),v.end()
// ll mod = 1000000007 ;
// long double pie = acos(-1) ;
// ll INF = 1000000000000 ;

// void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
// void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
// ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
// ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
// ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
// void mysort(vector<long long> &a){sort(a.begin(),a.end()) ;}
// void myrev(vector<long long> &a){reverse(a.begin(),a.end()) ;}
// void fix_cout(){cout << fixed << setprecision(20) ;}

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vvll dp(n + 1, vll(2019, 0));
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ll num = s.at(i - 1) - '0';
    dp.at(i).at(num)++;
    for (int j = 0; j < 2019; j++) {
      dp.at(i).at((j * 10 + num) % 2019) += dp.at(i - 1).at(j);
    }
    ans += dp.at(i).at(0);
  }
  cout << ans << endl;
}