fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <string>
  5. #include <set>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. #define NO cout << "NO\n";
  11. #define YES cout << "YES\n";
  12. #define loopi(n) for (int i = 0; i < n; i++)
  13. #define loopj(n) for (int j = 0; j < n; j++)
  14. #define loopi1(n) for (int i = 1; i <= n; i++)
  15. #define loopj1(n) for (int j = 1; j <= n; j++)
  16. #define pb push_back
  17. #define eb emplace_back
  18. #define graphinsertcheck(a, b) cout << "inserted: " << a << " to " << b << "\n"
  19. #define graphinsertweightcheck(a, b, c) cout << "inserted: " << a << " to " << b << " weight: " << c << "\n"
  20.  
  21.  
  22. typedef long long ll;
  23. typedef vector<vector<ll> > vvll;
  24. typedef vector<ll> vll;
  25. typedef pair<ll, ll> pll;
  26.  
  27. ll t, n, s;
  28. const ll INF = 1e9+7;
  29.  
  30. int main() {
  31. cin >> t;
  32. loopi(t) {
  33. cin >> n;
  34. vll set(n);
  35. loopj(n) cin >> set[j];
  36. sort(set.begin(), set.end()); // sort in ascending order
  37. ll sum = 0;
  38. for (int j = 1; j < n; j++) sum += set[j] * ((1LL << j) - 1); // sum contributions of 2nd biggest -> largest 1LL << i is the number of times its the largest in subset
  39. loopj(n-1) sum -= set[j] * ((1LL << (n - j - 1)) - 1); // subtract from smallest to 2nd biggest 1LL << n - i - 1 is # of times its the smallest in subset
  40. sum = sum % INF;
  41. cout << sum << "\n";
  42. }
  43. }
  44.  
Success #stdin #stdout 0s 5272KB
stdin
Set Difference  Add problem to Todo list
Problem code: SETDIFF  	
SUBMITMY SUBMISSIONSALL SUBMISSIONS
Read problems statements in Mandarin Chinese and Russian.

Churu is working as a data scientist in Coderpur. He works on a lot of data on the daily basis. One day, he found an interesting problem, which was very easy to solve for small data but was getting more complex with increasing number of data points. So, Churu needs your help in solving this problem.
Given a set S of N non-negative integers (Some integers might occur more than once in the set), find out the value of SETDIFF(S).
Mountain View
Where max(s) represents the maximum value in set s whereas min(s) represents the minimum value in the set s.
As value of SETDIFF(S) can be very large, print it modulo (109 + 7) .
Input

First line of input contains an integer T denoting number of test cases.
For each test case, first line will contain an integer N denoting number of elements in set S.
Next line contains N space separated integers denoting the set S.
Output

For each test case, print a single integer representing the answer of that test case.
Note

Two subsets will be called different if there exists an index i such that S[i] is occurs in one of the subset and not in another.
Constraints

Subtask #1: 20 points
1 ≤ T ≤ 5, 1 ≤ N ≤ 1000, 0 ≤ value in set ≤ 109
Subtask #2: 25 points
1 ≤ T ≤ 5, 1 ≤ N ≤ 105, 0 ≤ value in set ≤ 1000
Subtask #3: 55 points
1 ≤ T ≤ 5, 1 ≤ N ≤ 105, 0 ≤ value in set ≤ 109
Example

Input:
3
2
1 2
3
1 2 3
4
1 2 3 4
stdout
Standard output is empty