forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (41 loc) · 1.25 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// Source : https://leetcode.com/problems/sum-of-subsequence-widths/description/
/// Author : liuyubobobo
/// Time : 2018-08-19
#include <iostream>
#include <vector>
using namespace std;
/// Mathematics
/// Time Complexity: O(nlogn)
/// Space Complexity : O(1);
class Solution {
private:
long long MOD = 1e9 + 7;
public:
int sumSubseqWidths(vector<int>& A) {
sort(A.begin(), A.end());
int n = A.size();
long long res = (power(2ll, n - 1) - 1ll) * (long long)A.back() % MOD;
res = (res - (power(2ll, n - 1) - 1ll) * (long long)A[0] % MOD) % MOD;
for(int i = 1; i < A.size() - 1 ; i ++){
int left = i, right = n - (i + 1);
res = (res + (power(2ll, left) - 1ll) * (long long)A[i] % MOD) % MOD;
res = (res - (power(2ll, right) - 1ll) * (long long)A[i] % MOD) % MOD;
}
return res;
}
private:
long long power(long long a, long long b){
if(b == 0ll)
return 1ll;
long long t = power(a, b / 2ll);
long long ret = t * t % MOD;
if(b % 2ll == 1ll)
ret = ret * a % MOD;
return ret;
}
};
int main() {
vector<int> nums = {2, 1, 3};
cout << Solution().sumSubseqWidths(nums) << endl;
return 0;
}