点击打开题目链接https://www.codechef.com/problems/SETDIFF
Set DifferenceProblem code: SETDIFF |
All submissions for this problem are available.
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).
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) .
There might be repeated values in the set. For set S = {1,2,2}, consider that first 2 is not same as the second 2 and there will be two different subsets {1,2}. See last sample case for the more
clarifications.
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] 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: 4 2 1 2 3 1 2 3 4 1 2 3 4 3 1 2 2 Output: 1 6 23 3
Explanation
For first case answer will be 2-1 = 1.
For the second case:
Subset = {1}, max(s)-min(s) = 0.
Subset = {2}, max(s)-min(s) = 0.
Subset = {3}, max(s)-min(s) = 0.
Subset = {1,2}, max(s)-min(s) = 1.
Subset = {2,3}, max(s)-min(s) = 1.
Subset = {1,3}, max(s)-min(s) = 2.
Subset = {1,2,3}, max(s)-min(s) = 2.
So the output will be 1+1+2+2 = 6.
In the last case, there are three subsets, {1,2}, {1,2} and {1,2,2} having max(s) - min(s) = 1 for each.
求所有子集的 最大值减去最小值 的和 ,先对全集排个序,它的子集个数为2^n;其中,以{1,2,3,3
,5}为例,最大值是5,以5为最大值的子集个数为C(4,0)+C(4,1)+C(4,2)+C(4,3)+C(4,4),根据杨辉三角
的性质,C(n,0)+C(n,1)+.......+C(n,n)=2^n;所以C(4,0)+C(4,1)+C(4,2)+C(4,3)+C(4,4)=2^4;
以与5相邻的那个3为最大值的子集个数为C(3,0)+....C(3,3);。。。。以此类推;算出最大值的和
最小值同理;求出最大值的和与最小值的和,做个差就是答案;(别忘了那个+mod,具体看代码)。
#include<iostream> #include<cstdio> #include<algorithm> #include<string> #include<cstring> #include<queue> #include<cmath> #define maxn 100010 using namespace std; const int mod=1e9+7; typedef unsigned long long ull; ull p[maxn]; int arr[maxn]; int main() { int i; p[0]=1; for(i=1;i<maxn;++i) { p[i]=(p[i-1]*2)%mod; } int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); for(i=0;i<n;++i) { scanf("%d",&arr[i]); } sort(arr,arr+n); ull mmax=0; for(i=n-1;i>=0;--i) { mmax=(mmax+(arr[i]*p[i])%mod)%mod; } ull mmin=0; // for(i=0;i<n;++i)这样也行 // { // mmax=(mmax-(arr[i]*p[n-i-1])%mod+mod)%mod; // } for(i=0;i<n;++i) { mmin=(mmin+(arr[i]*p[n-i-1])%mod)%mod; } ull ans=(mmax-mmin+mod)%mod; //必须+mod,因为mmax、mmin都是对mod取余后的结果,谁大谁小不一定,而ans一定大于0 printf("%llu\n",ans); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。