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.
For each test case, print a single integer representing the answer of that test case.
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.
Subtask #1: 20 points
Subtask #2: 25 points
Subtask #3: 55 points
Input: 4 2 1 2 3 1 2 3 4 1 2 3 4 3 1 2 2 Output: 1 6 23 3
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.
http://www.codechef.com/MAY15/problems/SETDIFF
找所有子集合的max-min的和的值
解法:排序,找出所有的i,j(i<j),sum+=(num[j]-num[j])*pow(j-i-1)。然而,在这里,我们能找出一个巧妙的结论:
式子分解,前半部分系数num[1]=2^0,num[2]=2^0+2^1,num[3]=2^0+2^1+2^2
后半部分系数:num[2]=2^0,num[1]=2^0+2^1,num[0]=2^0+2^1+2^2 =2^3-1。于是可以简化。
#include<iostream> #include<algorithm> #include<string> #include<map> #include<vector> #include<cmath> #include<queue> #include<string.h> #include<stdlib.h> #include<stdio.h> #define ll long long #define mod 1000000007 using namespace std; ll x[100001]; int y[100001]; int main() { y[0]=1; for(int i=1;i<100000;++i){ y[i]=(y[i-1]*2)%mod; } int t; cin>>t; while(t--){ int n; cin>>n; for(int i=0;i<n;++i) cin>>x[i]; sort(x,x+n); ll s=0; for(int i=0;i<n;++i){ ll p=(x[i]%mod*((y[i]-1)-(y[n-1-i]-1))%mod)%mod; s=(s+p)%mod; } cout<<s<<endl; } return 0; }
这是我一开始的代码(20分):
#include<iostream> #include<algorithm> #include<string> #include<map> #include<vector> #include<cmath> #include<queue> #include<string.h> #include<stdlib.h> #include<stdio.h> #define ll long long #define mod 1000000007 using namespace std; ll x[100001]; ll y[100001]; int main(){ y[0]=1; for(int i=1;i<100000;++i) y[i]=(y[i-1]*2)%mod; int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); for(int i=0;i<n;++i) scanf("%I64d",&x[i]); sort(x,x+n); ll s=0; for(int i=0;i<n;++i){ for(int j=i+1;j<n;++j){ ll p=(x[j]-x[i])%mod; p=(p*y[j-i-1])%mod; s=(s+p)%mod; } } printf("%I64d\n",s); } return 0; }