2 solutions

  • 1
    @ 2026-3-15 14:36:55
    #include<bits/stdc++.h>
    typedef long long LL;
    using namespace std;
    const int N=2e5+10;
    LL ans=1e9+7;
    int a[N];
    int main()
    {
    	long long n;
    	cin>>n;
    	
    	LL cnt=0;
    	LL sum=0;
    	for(int i=0;i<n;i++)
    	{
    		cin>>a[i];
    		sum+=a[i];
    		sum=sum%ans;
    	}
    	for(int i=0;i<n;i++)
    	{
    		sum-=a[i];
    		if(sum<0) sum+=ans;
    		cnt+=sum*a[i];
    		cnt%=ans;
    	}
    	cout<<cnt;
    	return 0;
    }
    • 1
      @ 2026-3-15 14:06:19
      #include<bits/stdc++.h>
      typedef long long LL;
      using namespace std;
      const int P=1000000007;
      int main()
      {
      	int n;
      	cin>>n;
      	vector<int> a(n);
      	LL sum=0;
      	for(auto &x:a)
      	{
      		cin>>x;
      		sum+=x; //累积求和
      		sum%=P; //只保留余数
      	}
      	LL ans=0;
      	for(int i=0;i<n;i++)
      	{
      		sum-=a[i]; //更新后面的乘积
      		if(sum<0) sum+=P; //正余数
      		ans+=a[i]*sum;
      		ans%=P;
      	}
      	cout<<ans;
      	return 0;
      }
      
      • 1

      [ABC177C] 数对乘积之和(Sum of product of pairs)

      Information

      ID
      2712
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      5
      Tags
      # Submissions
      15
      Accepted
      6
      Uploaded By