k times interval Java

Related topics :

And the sub-array of K

Statistics [beautiful sub-array]

Title Description
Given a sequence of length N, A1, A2,...AN, if the sum of a continuous subsequence Ai, Ai+1,...Aj is a multiple of K, we call this interval [i,j] It is the K times interval.

Can you find the total number of K-fold intervals in the sequence?

Input format

The first line contains two integers N and K.
Each of the following N lines contains an integer Ai.

Output format

Output an integer, representing the number of K times interval.

data range

1≤N,K≤100000
1≤Ai≤100000

Input sample :

5 2
1
2
3
4
5

Sample output:

6

Idea
1. It is not difficult to read the questions carefully. This is the one-dimensional prefix sum problem: if the sum of the given array L to R is a multiple of K, then the answer is anscount++.
(If I don’t have much idea about the prefix and I found a blog that talks about the prefix and very well. The website puts DWVictor—the prefix and DWVictor wrote it in detail. If it is infringing, please tell me, I I just started to write a blog. Some of the rules are not very clear. If there is something wrong, I hope you can criticize and correct it.)
The data for this question is very large, and it must be timed out. The prefix sum just reduces the time complexity.
2. The sum of L to R is represented by sum[R]-sum[L-1], draw it on the draft paper, you can get it outInsert picture description here

3. With the above method as a guide, what we do next is very easy, just find out how many sum[R]-sum[L-1]%K==0there are, it is easy to think of a method double loop

  for(int i=1;i<=N;i++)
        for(int j=1;j<=i;j++){
    
    
           if((s[i]-s[j-1])%K==0&&s[i]-s[j-1]>0){
    
    
               
               count++;
           }

        }

However, this time will still exceed, and only partial points can be obtained. The reason is N<=100000that the worst-case double loop has to be executed to the power of 1 to the 10th power, which is greater than 100 million, so it has to be optimized.
4. Double loop timeout, we have to consider a loop, optimize a loop, if we want to optimize one, we must start with the judgment condition, because (sum[R]-sum[L-1])%K==0it is the most important code in the double loop, we can transform the formula
(sum[R]-sum[L-1])%K==0-> sum[R]%K==sum[L-1]%Kfrom We can get this formula: after taking the modulus of the prefix sum, the result is equal. The sum of the two prefixes can form a k times interval , which means that for a given K, if there are any two prefixes and %K, the result is equal, At this time, L and R can form a k times interval;
5. We use the count array to store the result of the fetch, count[i] represents the inumber of intervals of the fetched value , 我们要找的就是另一个值为i 的前缀和, if there is another anscount++ ; In this way, the double can be turned into a single, 1—N. For each prefix and sum[i]fetch, the result is the same, anscount++;
6. There is another point before the loop sum[0]%kis also equal to 0, so a prefix with a remainder of 0 has been There is one, so count[0]=1;
then all codes


import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner input=new Scanner(System.in);
        int N=input.nextInt();
        int K=input.nextInt();
        int count[]=new int [1000001];
        int arr[]=new int[N+1];
        int s[]=new int[N+1];
        for(int i=1;i<=N;i++){
    
    
            arr[i]=input.nextInt();
            s[i]=s[i-1]+arr[i];    //计算前缀和
            s[i]%=K;      //在这里直接对前缀和取摸
        }
        count[0]=1;
        long anscount=0;  //int 会爆掉

        for(int i=1;i<=N;i++)
        {
    
    
           anscount+=count[s[i]];//除了count[0]=1,其余的初始值都为0,                 
            count[s[i]]++;       //如果出现过一次后加1,出现过两次及以上,anscount++,      
            					 //我们就是要找总共有多少对,
            					 //只出现一次虽然count[i]加1但总的答案不会加
           }
        System.out.println(anscount);
    }
}

Finally, thank you for your guidance

Guess you like

Origin blog.csdn.net/qq_44844588/article/details/106915847