Source Code
#include <iostream>
#include <vector>
#include <map>
using namespace std;

int main()
{
    std::ios_base::sync_with_stdio(0); std::cin.tie(nullptr); std::cout.tie(nullptr);
    int t;cin>>t;

    for(int i=0;i<t;i++){
        int n;cin>>n;
        long long k,suffix=0,sum=0;cin>>k;
        long long ar[n];
        map<long long,int>pre;
        vector<long long>prefix;
        for(int j=0;j<n;j++){
            cin>>ar[j];
            if(j!=n-1){
                prefix.push_back(ar[j]);
                if(j!=0){
                    prefix[j]+=prefix[j-1];
                }
                pre[prefix[j]%k]++;
            }
        }

        //for(int j=0;j<n-1;j++){
          //  cout<<prefix[j]<<endl;
        //}
        int sufIndex=0,preIndex=n-2;
        vector<long long>suf(n-1);
        for(int j=n-1;j>=1;j--){
            suffix+=ar[j];
            suf[sufIndex]=suffix%k;
            sum+=pre[(k-suf[sufIndex])%k];

            pre[prefix[preIndex]%k]--;
            sufIndex++;
            preIndex--;
        }

        cout<<sum<<endl;
    }


    return 0;
}
Copy
Number of Ways YousefShaheen
GNU G++17
808 ms
29.9 MB
Accepted