Advertisement
LEGEND2004

Sum of product of pairs - sum

Aug 10th, 2023
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. const int mod = 1e9 + 7;
  6.  
  7. signed main()
  8. {
  9.     int n;
  10.     cin >> n;
  11.     int a[n + 5];
  12.     int sum = 0;
  13.     for(int i = 1; i <= n; i++){
  14.         cin >> a[i];
  15.         sum += a[i];
  16.         sum %= mod;
  17.     }
  18.     int ans = 0;
  19.     for(int i = 1; i <= n; i++){
  20.         sum = (sum + mod - a[i]) % mod;
  21.         ans += (sum * a[i]);
  22.         ans %= mod;
  23.     }
  24.     cout << ans << endl;
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement