AcWing 1230. K倍区间---java
原题链接
中等
作者:
Joma
,
2021-03-25 22:14:36
,
所有人可见
,
阅读 281
最后一个样例过不了的看看我的, b[i] 太大会导致求b[i] * (b[i] - 1)超过范围
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static int[] a = new int [100010];
static long[] b = new long [100010];
public static void main(String[] args) throws IOException{
String[] s1 = reader.readLine().split(" ");
int n = Integer.parseInt(s1[0]);
int k = Integer.parseInt(s1[1]);
long res = 0;
for (int i = 1; i <= n; i++) {
a[i] = (a[i - 1] + Integer.parseInt(reader.readLine())) % k;
b[a[i]] ++;
}
b[0] ++;
for(int i = 0; i < k ; i ++)
if(b[i] > 1) {
long temp = b[i] * (b[i] - 1) / 2;
res += temp;
}
System.out.println(res);
}
}