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. Python Web框架

    本节对Python Web框架学习 一.MTVModel: 存放所有数据库相关文件Template:模板文件,存放html文件View: 业务处理,即函数文件 二.MVCmodel: 存放数据库相关文 ...

  2. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  3. Zip4J最简单用法

    package com.chentao.MicroMessage.bussiness; import java.io.File; import java.util.ArrayList; import ...

  4. Kotlin——最详细的常量、变量、注释的使用

    在Kotlin中的变量.常量以及注释多多少少和Java语言是有着不同之处的.不管是变量.常量的定义方式,还是注释的使用.下面详细的介绍Kotlin中的变量.常量.注释的使用.以及和Java的对比. 如 ...

  5. jsDOM编程-小球在盒子里来回撞击

    首先写一个小页面:页面需要一个div 这个div就是盒子,然后在div里在包含一个子div 这个子div就包含一张小球的图片 代码: <!doctype html> <html> ...

  6. CPP--正码,反码,补码~附整数溢出的探讨

    之前说到了long的争议(http://www.cnblogs.com/dotnetcrazy/p/8059210.html),这边就不用long来举例了,用int吧 可以看一下这篇文章(http:/ ...

  7. 4.1 State Snapshot Transfer

    摘要: 出处:黑洞中的奇点 的博客 http://www.cnblogs.com/kelvin19840813/ 您的支持是对博主最大的鼓励,感谢您的认真阅读.本文版权归作者所有,欢迎转载,但请保留该 ...

  8. K:java中properties文件的读写

    Properties类与.properties文件:   Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...

  9. mayavi安装

    Mayavi是python的一个包,提供方便的可视化方案.目前(20150809)Mayavi还没有py3的支持,以下安装环境在python 2.7.10下进行 安装Mayavi: 1. 通过pip ...

  10. Linux入门篇(一)——基本命令

    这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...