182 字
1 分钟
题解:AT_abc405_c [ABC405C] Sum of Product
AT_abc405_c [ABC405C] Sum of Product 题解
题目大意
给定一个长度为 的整数序列 ,求 。
思路
显然可以暴力,但观察到 ,因此暴力是不可行的。
考虑优化。
观察到:
因此,可以推导出:
这样,我们就可以只计算 的总和和平方和,然后带进公式计算即可。
时间复杂度为 。
Code
#include<bits/stdc++.h>using namespace std;#define int long longint n,sum,sqsum;signed main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n; for (int i=1,x;i<=n;i++) cin>>x,sum+=x,sqsum+=x*x; cout<<(sum*sum-sqsum)/2; return 0;}题解:AT_abc405_c [ABC405C] Sum of Product
https://banglee13.github.io/posts/atabc405c/分享文章
生成精美分享图或复制链接,与更多人分享本文。
继续阅读
最后更新于 ,距今已过 561 天
部分内容可能已过时