Advertisement
LEGEND2004

Sum of product of pairs

Aug 8th, 2023 (edited)
1,303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 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.     vector<int> a(n);
  12.     vector<int> s(n);
  13.     for(int i = 0; i < n; i++){
  14.         cin >> a[i];
  15.     }
  16.     s[n - 1] = 0;
  17.     for(int i = (n - 2); i >= 0; i--){
  18.         s[i] = s[i + 1] + a[i + 1];
  19.         s[i] %= mod;
  20.     }
  21.     int ans = 0;
  22.     for(int i = 0; i < n; i++){
  23.         ans += (a[i] * s[i]);
  24.         ans %= mod;
  25.     }
  26.     cout << ans << endl;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement