C. Do you want a date?

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the same straight line, due to the reason that there is the only one straight street in Vičkopolis.

Let's denote the coordinate system on this street. Besides let's number all the hacked computers with integers from 1 to n. So the i-th hacked computer is located at the point xi. Moreover the coordinates of all computers are distinct.

Leha is determined to have a little rest after a hard week. Therefore he is going to invite his friend Noora to a restaurant. However the girl agrees to go on a date with the only one condition: Leha have to solve a simple task.

Leha should calculate a sum of F(a) for all a, where a is a non-empty subset of the set, that consists of all hacked computers. Formally, let's denote A the set of all integers from 1 to n. Noora asks the hacker to find value of the expression . Here F(a) is calculated as the maximum among the distances between all pairs of computers from the set a. Formally, . Since the required sum can be quite large Noora asks to find it modulo 109 + 7.

Though, Leha is too tired. Consequently he is not able to solve this task. Help the hacker to attend a date.

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) denoting the number of hacked computers.

The second line contains n integers x1, x2, ..., xn (1 ≤ xi ≤ 109) denoting the coordinates of hacked computers. It is guaranteed that all xi are distinct.

Output

Print a single integer — the required sum modulo 109 + 7.

Examples
Input
2
4 7
Output
3
Input
3
4 3 1
Output
9
Note

There are three non-empty subsets in the first sample test:, and . The first and the second subset increase the sum by 0 and the third subset increases the sum by 7 - 4 = 3. In total the answer is 0 + 0 + 3 = 3.

There are seven non-empty subsets in the second sample test. Among them only the following subsets increase the answer: , , , . In total the sum is (4 - 3) + (4 - 1) + (3 - 1) + (4 - 1) = 9.

题目链接:http://codeforces.com/contest/810/problem/C

分析:还是因为我太菜了,想了半天都没想出来怎么去写,上网查题解半天没有搞懂解题思路,过了一个小时灵光一现,答案出来了!

题意:给定集合,让我们求出所有子集中最大值最小值差的和

思路:很容易我们会想到直接排序,然后暴力枚举a[i]和a[j],那么他们之间的包含i位和j位的子集个数一定是2^(j-i-1)个,ans = 2^(j-i-1)*(a[j]-a[i])

但是一定会超时的啊,复杂度为O(n^2),所以我们考虑

长度为1结果是ans1 = a[1]-a[0]+a[2]-a[1] ....a[n-1]-a[n-2] = (a[1]+a[2]+...+a[n-1] )- (a[0]+a[1]+....+a[n-2])

长度为2是结果是ans2 = a[2]-a[0]+a[3]-a[1]+.....+a[n-1]-a[n-1-2]

这样我们可以清楚的发现ans = ans1+ans2+ans(n-1) = sum[n-1]-sum[i]-sum[n-1-i-1],这里sum[i]代表前i个的和

下面给出AC代码:

 #include<bits/stdc++.h>
using namespace std;
#define MAX 300000
#define mod 1000000007
long long a[MAX+],b[MAX+],ans=;
int main()
{
int n,i;
scanf("%d",&n);
b[]=;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=(*b[i-])%mod;
}
sort(a+,a+n+);
for(i=;i<=n;i++)
{
ans+=(b[i-]-b[n-i])*a[i]%mod;
ans%=mod;
}
cout<<ans<<endl;
}

Codeforces 810C Do you want a date?(数学,前缀和)的更多相关文章

  1. 【组合 数学】codeforces C. Do you want a date?

    codeforces.com/contest/810/problem/C [题意] 给定一个集合A,求 , 输入: [思路] 基数为n的集合有2^n-1个非空子集. 首先n个数要从小到大排序,枚举最后 ...

  2. 【codeforces 810C】Do you want a date?

    [题目链接]:http://codeforces.com/contest/810/problem/C [题意] 给你一个集合,它包含a[1],a[2]..a[n]这n个整数 让你求出这个集合的所有子集 ...

  3. Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学

    E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...

  4. Codeforces Round #207 (Div. 1)B(数学)

    数学so奇妙.. 这题肯定会有一个循环节 就是最小公倍数 对于公倍数内的相同的数的判断 就要借助最大公约数了 想想可以想明白 #include <iostream> #include< ...

  5. codeforces 2B The least round way(DP+数学)

    The least round way 题目链接:http://codeforces.com/contest/2/problem/B ——每天在线,欢迎留言谈论.PS.本题有什么想法.建议.疑问 欢迎 ...

  6. Codeforces Round #499 (Div. 2) C. Fly(数学+思维模拟)

    C. Fly time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  7. CodeForces - 586C Gennady the Dentist 模拟(数学建模的感觉)

    http://codeforces.com/problemset/problem/586/C 题意:1~n个孩子排成一排看病.有这么一个模型:孩子听到前面的哭声自信心就会减弱:第i个孩子看病时会发出v ...

  8. Codeforces Beta Round #11 B. Jumping Jack 数学

    B. Jumping Jack 题目连接: http://www.codeforces.com/contest/11/problem/B Description Jack is working on ...

  9. 【CodeForces】708 B. Recover the String 数学构造

    [题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...

随机推荐

  1. 【java】缓冲字符字节输入输出流:java.io.BufferedReader、java.io.BufferedWriter、java.io.BufferedInputStream、java.io.BufferedOutputStream

    BufferedReader最重要,因为有个方法public String readLine() package System输入输出; import java.io.BufferedReader; ...

  2. Overlapping rectangles判断两个矩形是否重叠的问题 C++

    Given two rectangles, find if the given two rectangles overlap or not. A rectangle is denoted by pro ...

  3. centOS7 jdk安装

    1.查找需要卸载的OpenJDK: #  rpm -qa | grep java 2:依次卸载 rpm -e --nodeps javapackages-tools-3.4.1-6.el7_0.noa ...

  4. css3毛玻璃模糊效果

    CSS3 blur滤镜实现如下测试代码: .blur { -webkit-filter: blur(10px); /* Chrome, Opera */ -moz-filter: blur(10px) ...

  5. @Autowired内部实现原理

    @Autowiredprivate CustomerDao customerDao;        public void addCustomer() {        customerDao.add ...

  6. 队列详解及java实现

    导读 栈和队列是有操作限制的线性表. 目录 1.队列的概念.特点.存储结构. 2.栈队列的java实现. 概念 队列是一种在一端进行插入,而在另一端进行删除的线性表.1.队列的插入端称为队尾:队列的删 ...

  7. linux保持管道中颜色显示

    在linux工作中,不同类型的文件以不同的颜色显示,如文件夹显示蓝色,压缩文件显示橘黄色,可执行文件显示为绿色,链接失效文件高亮显示等等: 有时候根据颜色可以快速鉴别,如我有时为了保持目录的完整性,会 ...

  8. Java Web高级编程(三)

    使用过滤器改进应用程序 一.过滤器的目的 过滤器是可以拦截访问资源的请求.资源的响应或者同时拦截两者的应用组件.过滤器可以检测和修改请求和响应,同时也可以拒绝.重定向或转发请求.javax.servl ...

  9. JavaScript 浏览器类型及版本号

    项目中偶尔用到判断浏览器类型及相关版本问题,现记录相关代码: function getBrowserVertion(userAgent) { var browserName, browserVersi ...

  10. linux命令和知识点

    一.常用命令 $?    上个命令的退出状态,或函数的返回值. 二.数字判断 [ $count -gt "1"] 如果$count 大于1 为真 -gt  大于 -lt    小于 ...