Print the answer.
Sample Input 1
3 2 8 4
Sample Output 1
56
We have ∑Ni=2∑i−1j=1(Ai−Aj)^2=(8−2)^2+(4−2)^2+(4−8)^2=56.
Sample Input 2
5 -5 8 9 -4 -3
Sample Output 2
950
这个题是个推公式题,看这个数据范围暴力肯定wa
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int maxn=1e6+100; int n; ll a[maxn]; ll ai[maxn]; ll aj[maxn]; ll sum[maxn]; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; sum[i]=sum[i-1]+a[i]; } ll sum1=0; ll sum2=0; ll sum3=0; for(int i=2;i<=n;i++){ sum1+=(a[i]*a[i]*(i-1)); } for(int i=1;i<=n;i++){ sum2+=(a[i]*a[i]*(n-i)); } for(int i=2;i<=n;i++){ sum3+=2*(a[i]*sum[i-1]); } cout<<sum1+sum2-sum3<<endl; }