数论推公式

 

 

 

 

Print the answer.

Sample Input 1

 

3
2 8 4

Sample Output 1

 

56

We have Ni=2i1j=1(AiAj)^2=(82)^2+(42)^2+(48)^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;
}

 

posted @ 2021-03-10 18:18  lipu123  阅读(47)  评论(0编辑  收藏  举报