代码如下
using ll = long long;
int main() {ll n, m,ans=0,i;std::cin >> n >> m;std::vector<ll>a(m+1);for (int i = 1; i <= m; i++) {std::cin >> a[i];a[i] += a[i - 1];}//如果m<n,那么只够写第一篇文章ans += a[1] * std::min(m,n);for (i = n; i <= m; i+=n) {//如果m>n,最后一次是m-i,前面是n,从i=n开始,看有多少个n.ans += (a[i+1] - a[i - n+1]) * std::min(m-i,n);}std::cout << ans << '\n';return 0;
}
模版,初始取c项,总共m项,每次跳跃n项
ans += a[c] * std::min(m,n);
for (i = n+c-1; i <= m; i+=n) {//计算的内容乘以段长.ans += (计算的内容) * std::min(m-i,n);
}
std::cout << ans << '\n';
对于前缀和
using ll = long long;
int main() {ll n, m,ans=0,i;std::cin >> n >> m;std::vector<ll>a(m+1);for (int i = 1; i <= m; i++) {std::cin >> a[i];a[i] += a[i - 1];}//如果m<n,那么只够写第一篇文章ans += a[c] * std::min(m,n);for (i = n+c-1; i <= m; i+=n) {//总共n项,左边n项的和,右边剩余的mans += (a[i+c] - a[i - n+c]) * std::min(m-i,n);}std::cout << ans << '\n';return 0;
}