题解:B4105 [CSP-X2024 山东] 消灭怪兽
FJ_EYoungOneC · · 题解
原题链接
P8649 [蓝桥杯 2017 省 B] k 倍区间
B4105 [CSP-X2024 山东] 消灭怪兽
解题思路
20pts
我们可以枚举区间左端点
40pts
我们可以使用前缀和算法优化上述统计
100pts
我们用
所以我们要计算以
时间复杂度
AC_Code
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int n, k;
int h[N];
int main()
{
cin >> n >> k;
h[0] ++;
LL s = 0, res = 0;
for (int i = 1; i <= n; ++ i )
{
int x;
cin >> x;
s += x;
res += h[s % k] ++;
}
cout << res << endl;
return 0;
}