UVA 12220 - Divisible Subsequences Solution
Problem Link : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3372
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- const int mx = 5e4 + 9;
- using ll = long long;
- ll pre[mx];
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n, d;
- cin >> d >> n;
- int a[n + 1];
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- }
- pre[0] = 0;
- for (int i = 1; i <= n; i++) {
- pre[i] = a[i] + pre[i - 1];
- }
- ll ans = 0;
- map<int, int> cnt;
- cnt[pre[0] % d]++;
- for (int l = 1; l <= n; l++) {
- ans += cnt[pre[l] % d];
- cnt[pre[l] % d]++;
- }
- cout << ans << "\n";
- }
- return 0;
- }
No comments