Problem p02851 (C++)

Buggy submission (status: Runtime Error):

// Problem : E - Rem of Sum is Num
// Contest : AtCoder - AtCoder Beginner Contest 146
// URL : https://atcoder.jp/contests/abc146/tasks/abc146_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
#define int long long
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 100005

// Solution

int n, k;
int a[N];
mi m;

int get(int a, int b) {
  // return (a-b+k)%k;
  int x = a - b;
  while (x < 0)
    x += k;
  return x % k;
}

void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i] += a[i - 1];
  m[0]++;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i >= k)
      m[get(a[i - k], i - k)]--;
    ans += m[get(a[i], i)];
    m[get(a[i], i)]++;
  }
  cout << ans << endl;
}

signed main() {
  io;
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  int tests;
  // cin>>tests;
  tests = 1;
  while (tests--) {
    solve();
  }
  return 0;
}

Fixed submission:

// Problem : E - Rem of Sum is Num
// Contest : AtCoder - AtCoder Beginner Contest 146
// URL : https://atcoder.jp/contests/abc146/tasks/abc146_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
#define int long long
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 200005

// Solution

int n, k;
int a[N];
mi m;

int get(int a, int b) {
  // return (a-b+k)%k;
  int x = a - b;
  while (x < 0)
    x += k;
  return x % k;
}

void solve() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i], a[i] += a[i - 1];
  m[0]++;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i >= k)
      m[get(a[i - k], i - k)]--;
    ans += m[get(a[i], i)];
    m[get(a[i], i)]++;
  }
  cout << ans << endl;
}

signed main() {
  io;
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  int tests;
  // cin>>tests;
  tests = 1;
  while (tests--) {
    solve();
  }
  return 0;
}