题解:CF1921F Sum of Progression
zhangxiaoyu008 · · 题解
首先我们考虑暴力。
暴力 1
暴力枚举每个数将其乘以对应系数计入答案。
单次最坏复杂度
所以总的最坏复杂度为
暴力 2
令
所以
于是,你就成功的学会了根号分治。
:::info[
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5, M = 450;
int T, n, q, B, s, d, k, a[N];
LL sum[M][N], sum2[M][N], res;
int main()
{
scanf("%d", &T);
while(T --) {
scanf("%d%d", &n, &q);
B = max(1, (int)sqrt(q));
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
for(int j = 1; j <= B; j ++)
for(int i = n; i; i --) {
sum[j][i] = a[i];
if(i + j <= n) sum[j][i] = sum[j][i + j] + a[i];
}
for(int j = 1; j <= B; j ++)
for(int i = n; i; i --) {
sum2[j][i] = a[i];
if(i + j <= n) sum2[j][i] += sum2[j][i + j] + sum[j][i + j];
}
while(q --) {
scanf("%d%d%d", &s, &d, &k);
res = 0;
if(d <= B) {
res = sum2[d][s];
if(s + d * k <= n) res -= sum2[d][s + d * k] + k * sum[d][s + d * k];
}
else for(int i = 1, j = s; i <= k; i ++, j += d) res += (LL)i * a[j];
printf("%lld ", res);
}
puts("");
}
return 0;
}
:::