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

/*
ID: Aman Raj
LANG: C++
TASK: nocows
*/
// #include<iostream>
#include <stdio.h>
// #include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
#include <math.h>
typedef long long ll;
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16); cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints
// xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;

#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
// #define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod 2019
// #define mod 3
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define flush fflush(stdout)
#define primeDEN 727999983
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#include <fstream>
// find_by_order()  // order_of_key
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

/*bool comp(pair<ll,ll> p1, pair<ll,ll> p2)
{
 if(p1.first!=p2.first)return p1.first < p2.first;
  else return p1.second < p2.second;
}*/

ll power1(ll a, ll b) // a raised to power b
{
  if (b <= 0)
    return 1;
  if (b == 1)
    return a;
  ll c = power1(a, b / 2);
  c *= c;
  c %= mod;
  if (b % 2 == 1)
    c *= a;
  c %= mod;
  return c;
}
ll mi(ll a, ll m) {
  ll c = power1(a, m - 2);

  return c;
}

ll findpower(ll a, ll b) {
  ll i = 0;
  ll c = 1;
  while (1) {
    c *= 2;

    if (c > a)
      return i;
    i++;
  }
}

/*
ll gcd(ll a,ll b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define MAXN 1000001

// stores smallest prime factor for every number
int spf[MAXN];
void sieve()
{
     spf[1] = 1;
     for (int i=2; i<MAXN; i++)

             // marking smallest prime factor for every
             // number to be itself.
             spf[i] = i;

     // separately marking spf for every even
     // number as 2
     for (int i=4; i<MAXN; i+=2)
             spf[i] = 2;

     for (int i=3; i*i<MAXN; i++)
     {
             // checking if i is prime
             if (spf[i] == i)
             {
                     // marking SPF for all numbers divisible by i
                     for (int j=i*i; j<MAXN; j+=i)

                             // marking spf[j] if it is not
                             // previously marked
                             if (spf[j]==j)
                                     spf[j] = i;
             }
     }
}*/

int main() {
  fast;
  string s;
  cin >> s;
  ll n = s.size();

  ll ans = 0;
  vector<ll> dp(n);
  dp[n - 1] = s.back() - '0';
  vector<ll> rem(2019);
  rem[dp[n - 1]]++;
  rem[0]++;
  for (ll i = n - 2; i >= 0; i--) {
    ll a = s[i] - '0';
    dp[i] = ((dp[i + 1] + a * power1(10, n - i - 1)) % mod);
    // cout<<dp[i]<<endl;

    rem[dp[i]]++;
    ans += rem[dp[i]] - 1;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

/*
ID: Aman Raj
LANG: C++
TASK: nocows
*/
// #include<iostream>
#include <stdio.h>
// #include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
#include <math.h>
typedef long long ll;
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// setbase - cout << setbase (16); cout << 100 << endl; Prints 64
// setfill -   cout << setfill ('x') << setw (5); cout << 77 << endl; prints
// xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
// cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
using namespace __gnu_pbds;

#define f(i, a, b) for (i = a; i < b; i++)
#define rep(i, n) f(i, 0, n)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define ss second
#define ff first
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
// #define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sz(a) a.size()
#define inf (1000 * 1000 * 1000 + 5)
#define all(a) a.begin(), a.end()
#define tri pair<int, pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod 998244353
// #define mod 3
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vi, greater<int>>
// #define flush fflush(stdout)
#define primeDEN 727999983
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#include <fstream>
// find_by_order()  // order_of_key
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

/*bool comp(pair<ll,ll> p1, pair<ll,ll> p2)
{
 if(p1.first!=p2.first)return p1.first < p2.first;
  else return p1.second < p2.second;
}*/

ll power1(ll a, ll b) // a raised to power b
{
  if (b <= 0)
    return 1;
  if (b == 1)
    return a;
  ll c = power1(a, b / 2);
  c *= c;
  c %= mod;
  if (b % 2 == 1)
    c *= a;
  c %= mod;
  return c;
}
ll mi(ll a, ll m) {
  ll c = power1(a, m - 2);

  return c;
}

ll findpower(ll a, ll b) {
  ll i = 0;
  ll c = 1;
  while (1) {
    c *= 2;

    if (c > a)
      return i;
    i++;
  }
}

/*
ll gcd(ll a,ll b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define MAXN 1000001

// stores smallest prime factor for every number
int spf[MAXN];
void sieve()
{
     spf[1] = 1;
     for (int i=2; i<MAXN; i++)

             // marking smallest prime factor for every
             // number to be itself.
             spf[i] = i;

     // separately marking spf for every even
     // number as 2
     for (int i=4; i<MAXN; i+=2)
             spf[i] = 2;

     for (int i=3; i*i<MAXN; i++)
     {
             // checking if i is prime
             if (spf[i] == i)
             {
                     // marking SPF for all numbers divisible by i
                     for (int j=i*i; j<MAXN; j+=i)

                             // marking spf[j] if it is not
                             // previously marked
                             if (spf[j]==j)
                                     spf[j] = i;
             }
     }
}*/

int main() {
  fast;
  string s;
  cin >> s;
  ll n = s.size();
  vector<vector<vector<ll>>> dp(n + 1, vector<vector<ll>>(673, vector<ll>(3)));
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ll a = s[i - 1] - '0';
    for (ll j = 0; j < 673; j++) {
      for (ll k = 0; k < 3; k++) {
        dp[i][(10 * j + a) % 673][(10 * k + a) % 3] += dp[i - 1][j][k];
      }
    }
    dp[i][a][(a % 3)]++;
    ans += dp[i][0][0];
  }
  cout << ans << endl;

  return 0;
}