Meaning of the questions:
In a sequence of positive integers length of $ N $ $ \ {a_i \} $, the definition of a "legal" sub-range, and which can be divisible by $ K $. Seeking original sequence includes the number of "legal" sub-section?
A total of $ T $ data set.
$ 1 \ T \ 20, \ quad 1 \ N \ 5 * 10 ^ 4, \ quad 1 \ K \ 10 ^ 6, \ quad 1 \ the a_i \ 10 ^ 9 $
analysis:
At first glance unexpected $ O (n \, logn) $ approach to write about $ O (n) \ sim O (n ^ 2) $ violent it.
Here $ O (n) $ prefix and the pretreatment time, $ O (n ^ 2) $ is an enumeration endpoint time interval. Every time found a "legal" sub-section on $ ans ++ $.
Consider, we do this is based on the equation: $$ pre [r] -pre [l-1] = \ sum \ limits ^ {r} _ {i = l} a_i \ equiv 0 \; (mod \ , K) $$
Then adapt it about, clearly $$ pre [r] \ equiv pre [l-1] \; (mod \, K) $$
Dying disease in shock and sat up, this question open barrel I see the line! Interval any two locations prefixes and die with more than $ K $ as the endpoint is composed of "legal", so we opened the barrel of each mold $ K $ remainder are counted, the number of combinations using seek answers.
Time complexity O (K), the space complexity of O (N + K).
Implementation (100):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define IL inline using namespace std; const int N=5e4; const int K=1e6; typedef long long LL; int n,T; LL k,a[N+3],s[N+3],c[N+3],ans; int cnt[K+3]; int main () { c[0]=c[1]=0; c[2]=1; for(int i=3;i<=N;i++) c[i]=c[i-1]+i-1; scanf("%d",&T); while(T--){ scanf("%lld%d",&k,&n); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); memset(cnt,0,sizeof cnt); s[0]=0; cnt[0]=1; for(int i=1;i<=n;i++){ s[i]=(s[i-1]+a[i])%k; cnt[s[i]]++; } ans=0; for(int i=0;i<k;i++) years + = c [cnt [i]]; printf("%lld\n",ans); } return 0; }
summary:
For the count, we can usually put the principle of deformation equation, find the law, can be violent from $ ans ++ $ evolved.