[Explanations] (thinking) k divisible by the number of substrings

Description

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

 

Sample Input

2 
7 3 
1 2 3 
4 8 
2 1 2 1 1 2 1 2 

Sample Output

0 
6 
 

Hint

Data satisfies 100%

. 1 <= T <= 20 is

. 1 <= N <= 50000

. 1 <= K <= 1000000

each number sequence <= 1000000000

30% data satisfies

. 1 <= T <= 10

. 1 <= N, K <= 1000


 

To a sub-segment k is divisible by only two of the prefix and the% k meaning the lower phase was reduced to 0

k is small, can start recording a bucket% k value, for the same value as long as the two can be picked from the inside, so that the contribution of each value of the answer is $ C ^ {2} _ {b [i]} $

Note that the original can be included in a separate answer 0, and finally add about

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const int maxn=50009;
int n,t,b[1000009];
ll sum[maxn],x,ans,k;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lld%d",&k,&n);ans=0;
        memset(sum,0,sizeof(sum));
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        scanf("%lld",&x),sum[i]=(sum[i-1]+x)%k,b[sum[i]]++;
        for(int i=0;i<=k-1;i++)ans+=b[i]*(b[i]-1)/2;
        printf("%lld\n",ans+b[0]);
    }
}

 

Guess you like

Origin www.cnblogs.com/superminivan/p/11285372.html
Recommended