Problem p02702 (C++) — buggy submission, verdict: Runtime Error


// </> : towhid1zaman

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define fill(a, b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define sp(k) cout << setprecision(k) << fixed;
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a, b) for (int i = (a); i <= (b); ++i)
#define irep(i, b, a) for (int i = (b); i >= (a); --i)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define _ios                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int maxn = 2000100;

ll mbxp(ll x, ll n, ll m) {
  ll res = 1;
  x %= m;
  while (n > 0) {
    if (n % 2 == 1) {
      res = (res * x) % m;
    }
    n /= 2;
    x = (x * x) % m;
  }
  return res % m;
}
int main() {
  _ios;

  string s;
  cin >> s;
  reverse(all(s));
  ll n = s.size();
  vi a(n);
  for (ll i = 0; i < n; i++) {
    a[i] = ((s[i] - '0') * mbxp(10, i, 2019)) % 2019;
  }
  vi pref(n);
  pref[0] = a[0];
  for (ll i = 1; i <= n; i++) {
    pref[i] = (pref[i - 1] + a[i]) % 2019;
  }
  map<ll, ll> mp;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (pref[i] == 0)
      ans++;
    ans += mp[pref[i]];
    mp[pref[i]]++;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:


// </> : towhid1zaman

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define fill(a, b) memset(a, b, sizeof(a))
#define all(v) (v).begin(), (v).end()
#define sp(k) cout << setprecision(k) << fixed;
#define rep(i, a) for (int i = 0; i < a; i++)
#define rep1(i, a, b) for (int i = (a); i <= (b); ++i)
#define irep(i, b, a) for (int i = (b); i >= (a); --i)
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define unq(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define _ios                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define sqr(a) ((a) * (a))
#define sz(a) int(a.size())
#define ff first
#define ss second
#define pb push_back

const double pi = acos(-1.0);
const int mod = 1000000007; // (int)1e9+7
const int maxn = 2000100;

ll mbxp(ll x, ll n, ll m) {
  ll res = 1;
  x %= m;
  while (n > 0) {
    if (n % 2 == 1) {
      res = (res * x) % m;
    }
    n /= 2;
    x = (x * x) % m;
  }
  return res % m;
}
int main() {
  _ios;

  string s;
  cin >> s;
  reverse(all(s));
  ll n = s.size();
  vll a(n);
  for (ll i = 0; i < n; i++) {
    a[i] = ((s[i] - '0') * mbxp(10, i, 2019)) % 2019;
  }
  vll pref(n);
  pref[0] = a[0];
  for (ll i = 1; i <= n; i++) {
    pref[i] = (pref[i - 1] + a[i]) % 2019;
  }
  map<ll, ll> mp;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (pref[i] == 0)
      ans++;
    ans += mp[pref[i]];
    mp[pref[i]]++;
  }
  cout << ans << endl;

  return 0;
}