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


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::string S;
  std::cin >> S;
  int n = sz(S);

  ll ans = 0;
  std::vector<std::vector<ll>> dp(2, std::vector<ll>(2019));
  rep(i, n) {
    ll t = S[i] - '0';
    dp[i % 2][t] += 1;
    if (i == 0)
      continue;
    rep(j, 2019) dp[i % 2][(10 * j + t) % 2019] += dp[(i - 1) % 2][j];
    ans += dp[i % 2][0];
    rep(j, 2019) dp[(i - 1) % 2][j] = 0;
  }
  std::cout << ans << std::endl;
  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt,
// less) : if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*"))
// ) std::cout << "YES" << std::endl;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::string S;
  std::cin >> S;
  int n = sz(S);

  std::vector<std::vector<ll>> dp(n, std::vector<ll>(2019));
  rep(i, n) {
    ll t = S[i] - '0';
    rep(j, 2019) {
      if (i == 0) {
        if (j == t)
          dp[i][j] = 1;
        else
          dp[i][j] = 0;
      } else {
        if (j == t)
          dp[i][j] += 1;
        dp[i][(10 * j + t) % 2019] += dp[i - 1][j];
      }
    }
  }

  // rep(i,n) rep(j,2019) if ( dp[i][j] != 0 ) std::cout << i << " " << j << " ;
  // " << dp[i][j] << std::endl;

  ll ans = 0;
  rep(i, n) ans += dp[i][0];
  std::cout << ans << std::endl;
  return 0;
}