Blue Bridge - K times interval

The meaning of the question: Give you a sequence and ask you how many interval elements the sum is a multiple of K.
Ideas: Count the prefix sum from front to back and how many %k there are in this segment.
The following is the core code:

	rep(i,1,n){
    
    
        sd(a[i]);
        a[i]+=a[i-1];
        a[i]%=k;
        ans+=cnt[a[i]];
        cnt[a[i]]++;
    }

This is not complete, you also need to add 1~i to 0, that is, from the first to the present, the sum is a multiple of k

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define sd(a) scanf("%d",&a)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define cl(a,b) memset(a,b,sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define dbg() printf("aaa\n")
using namespace std;
const int maxn=1e5+10;
int a[maxn];
int cnt[maxn];
int main() {
    
    
	int n,k;
    sdd(n,k);
    a[1]=0;
    cl(cnt,0);
    ll ans=0;
    rep(i,1,n){
    
    
        sd(a[i]);
        a[i]+=a[i-1];
        a[i]%=k;
        ans+=cnt[a[i]];
        cnt[a[i]]++;
    }
    printf("%lld\n",ans+cnt[0]);
	return 0;
}//挺好的题
//https://blog.csdn.net/qq_34594236/article/details/70845223?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

Problem Description
Given a sequence of length N, A1, A2, … AN, if the sum of a continuous subsequence Ai, Ai+1, … Aj (i <= j) is a multiple of K, we call this The interval [i, j] is K times the interval.

Can you find the total number of K-fold intervals in the sequence?
Input
The first line contains two integers N and K. (1 <= N, K <= 100000)
Each of the following N lines contains an integer Ai. (1 <= Ai <= 100000)
Output
Output an integer representing the number of K times the interval.
sample input

5 2
1
2
3
4
5

Sample output

6

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326145469&siteId=291194637