Source Code
#include <iostream>
#include <iomanip>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <set>
#include <cstdio>
#include <string>
#include <stack>
#include <map>
#include <sstream>

#define ll long long
#define sz(s) (int)s.size()
#define all(x) x.begin(), x.end()
using namespace std;
void AIA()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}
void file()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}
const int OO = 1e9 + 7;
const long double PI = acos(-1);
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dx4[] = {0, 1, 0, -1};
int dy[] = {1, -1, 0, 0, 1, -1, -1, 1}, dy4[] = {1, 0, -1, 0};
int main()
{
    // Verify Your thought before Coding.
    AIA();
    file();
    int t;
    cin >> t;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        vector<ll> pre(n + 1), suf(n + 2);
        for (int i = 1; i <= n; i++)
            cin >> pre[i];
        for (int i = n; i >= 1; i--)
            suf[i] += suf[i + 1] + pre[i];
        for (int i = 1; i <= n; i++)
            pre[i] += pre[i - 1];
        map<int, int> mp;
        for (int i = 1; i <= n; i++)
            mp[pre[i] % k]++;
        ll cnt = 0;
        for (int i = n; i >= 1; i--)
        {
            mp[pre[i] % k]--;
            cnt += mp[(k - (suf[i] % k)) % k];
            // cout << mp[(k - (suf[i] % k)) % k] << " ";
        }
        cout << cnt << "\n";
    }
}
Copy
Number of Ways kareemabdelrhmane
GNU G++17
928 ms
22.2 MB
Accepted