jzoj 2644. the number of columns

Give you a sequence of length N is a positive integer, if a continuous sequence, promoter sequence and the entire K can be

removed, then this sequence, as the legitimate demand of the original sequence, including how many legitimate continuous sequence?

For a sequence of length 8, K = 4 in the case of: 2, 1, 2, 1, 1, 2, 1, 2. It answers to 6, the subsequence

is a position 1-> position 8, a 2> 4,2-> 7,3-> 5,4> 6,5> 7.

 

Input

First line: T, represents a data set

for each set of data:

The first line: the number 2, K, N

of the second row: N number representing the sequence

Output

Total T lines, each line represents a number of answers

 

 
Simply add the sort of number theory. . .
We use the prefix and storage. (I.e., from 1 to x and sequence)
Then, we are looking for can be divisible by k intervals and,
There is S1 <S2
So as to satisfy S1≡S2 (mod k)
According congruence with
(S1-S2)≡0(mod k)
That is, when an equal number of S1 and S2 mold k I, k may be divisible composition range.
And mold can prefix k, then sort, combined (connection method that is not a high school, elementary school on it ...)
Or together with the barrel, ans + = t * (t-1) / 2
Remember that all must take to join the remainder of 0 ans alone, he was very special
#include<bits/stdc++.h>
using namespace std;

int sum[50500],k,t,n;

int main(){
    scanf("%d",&t);
    for(int o=1;o<=t;o++){
        scanf("%d%d",&k,&n);
        sum[0]=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&sum[i]);
            sum[i]=(sum[i]+sum[i-1])%k;
        }
        sort(sum+1,sum+1+n);
        int tt=0;
        sum[0]=-10086;
        int ans=0;
        for(int i=1;i<=n;i++){
            if(sum[i]==0)ans++;
            if(sum[i]==sum[i-1]){
                tt++;
                ans+=tt;
            }
            else tt=0;
        }
        printf("%d\n",ans);
    }
    
    
    return 0;
}
View Code

 

 

Guess you like

Origin www.cnblogs.com/you-xiao-mang-ci/p/11285360.html