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


/* You are being watched. */

//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")

using namespace std;

//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\\n"
#define FastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using ll = int;
using ld = long double;
using uint = unsigned int;
using ull = unsigned int;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \\n"[sizeof...(args) == 0];
  print(args...);
}

const ll MOD = 2019;

void solve() {
  string s;
  cin >> s;
  int n = s.size();
  ll ans = 0;
  reverse(s.begin(), s.end());
  map<int, int> cnt;
  cnt[0] = 1;
  ll cur = 0;
  ll p = 1ll;
  for (int i = 0; i < n; i++) {
    cur = (cur + (s[i] - '0') * p) % 2019;
    p = (p * 10) % 2019;
    ans += cnt[cur];
    cnt[cur]++;
  }
  print(ans);
}

int main() {
  int t;
  t = 1;
  while (t--)
    solve();
}

Error: TLE

Fixed submission:


/* You are being watched. */

//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")

#include <bits/stdc++.h>

//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")

using namespace std;

//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\\n"
#define FastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

using ll = int;
using ld = long double;
using uint = unsigned int;
using ull = unsigned int;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

double getTime() { return clock() / (double)CLOCKS_PER_SEC; }

void read(){};

template <typename T, typename... Args> void read(T &a, Args &...args) {
  cin >> a;
  read(args...);
}

void print(){};

template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " \\n"[sizeof...(args) == 0];
  print(args...);
}

const ll MOD = 2019;

void solve() {
  string s;
  cin >> s;
  int n = s.size();
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll rem = 0;
    ll pf = 0;
    unordered_map<int, int> cnt[n];
    for (int j = i; j < n; j++) {
      rem = (rem * 10ll + (s[j] - '0')) % MOD;
      if (cnt[i].count(rem))
        break;
      cnt[j][rem]++;
      if (rem == 0)
        pf++;
    }
    ans += (pf * (pf + 1)) / 2ll;
  }
  print(ans);
}

int main() {
  int t;
  t = 1;
  while (t--)
    solve();
}