-
Notifications
You must be signed in to change notification settings - Fork 0
/
29. Subarray Divisibility.cpp
52 lines (47 loc) · 1.4 KB
/
29. Subarray Divisibility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "iostream"
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef set<ll> sl;
typedef map<string, ll> msl;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pll;
#define fastio() ios_base::sync_with_stdio(0);cin.tie(NULL)
#define PB push_back
#define MP make_pair
#define LB lower_bound
#define UB upper_bound
#define F first
#define S second
#define I iterator
#define REP(i,a,b) for (ll i = a; i < b; i++)
#define REPR(i,a,b) for (ll i = a; i > b; i--)
#define REPJ(i,a,b,j) for (ll i = a; i < b; i+=j)
#define REPIT(it,o) for (auto it = o.begin(); it != o.end(); it++)
#define REPITR(it,o) for (auto it = o.rbegin(); it != o.rend(); it++)
#define ALL(a) a.begin(),a.end()
#define ALLR(a) a.rbegin(),a.rend()
int main() {
fastio();
ll n; cin >> n;
vl a(n);
REP(i,0,n){ cin >> a[i]; }
vl mods(n,0);
// mods[0] = 1;
ll cur = 0; ll ans = 0;
REP(i,0,n){
cur = (cur%n+a[i]%n+n)%n;
if (cur == 0){ ans++; }
ans += mods[cur];
mods[cur]++;
}
cout << ans;
}
// - initial approach
// maitain the count of modulo sum of every subarray that starts at 0;loop over and add the count of modulo upto that ele to ans, because we can remove that subarray from the start to get the desired subarray
// ac