Codeforces 809A - Do you want a date?(数学+排序)

A. 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 allxi are distinct.

Output

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

Examples

input

24 7

output

3

input

34 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 0and 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.

题意:给一个长度为n的数组(数组元素没有重复,可以看成一个集合),求这个集合的子集中最大值和最小值的差,然后求和。

我们知道n个元素的集合的子集为2^n个,把数组从小到大排序后,任意的第i个元素和第j个元素(i<j)可以算出关系为2^(j-i-1)*(a[j]-a[i]),然后根据这个式子可以写出代码。

代码:

for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            ans+=b[j-i-1]*(a[j]-a[i])%mod;
            ans%=mod;
        }
    }//数组b为2^(n-1)

然而这个算法的时间复杂度为O(n^2),很明显地会超时,不符合题目要求。

然后对a[i]这个元素进行观察和推算,可以推出差值为(2^(i-1)-2^(n-i))*a[i]。

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 3e5 + 5;
typedef long long LL;
LL a[maxn], b[maxn];
#define mod 1000000007
int main()
{
    int n;
    while (cin >> n)
    {
        int i;
        b[0] = 1;
        for (i = 1; i <= n; i++)
        {
            cin >> a[i];
            b[i] = (2 * b[i - 1]) % mod;
        }
        sort(a + 1, a + 1 + n);
        LL ans = 0;
        for (i = 1; i <= n; i++)
            ans = (ans + (b[i - 1] - b[n - i])*a[i]) % mod;
        cout << ans << endl;
    }
    return 0;
}
时间: 2024-10-10 13:32:41

Codeforces 809A - Do you want a date?(数学+排序)的相关文章

codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2). DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ...,

CodeForces 577C Vasya and Petya&#39;s Game 数学

题意就是给你一个1到n的范围 你每次可以问这个数是否可以被某一个数整除 问你要猜多少数才能确定这个数…… 一开始一点思路也没有 后来查了一下才知道 每个数都可以分为几个质数的整数次幂相乘得到…… 1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<math.h> 5 #include<string.h> 6 #include<string>

CodeForces 55D Beautiful numbers(数位dp+数学)

题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道数位dp,就是满足一个数能被所有位数的lcm整除即可. 一般都会设dp[len][mod][LCM],mod表示余数,LCM表示前len位的lcm. 但是如果直接裸mod会很复杂,于是再想lcm{0,1,2,3,4,5,6,7,8,9}=2520; 而且lcm{a,b,c,d....}{a,b,c,

codeforces 264 B. Good Sequences(dp+数学的一点思想)

题目链接:http://codeforces.com/problemset/problem/264/B 题意:给出一个严格递增的一串数字,求最长的相邻两个数的gcd不为1的序列长度 其实这题可以考虑一下素因数,将每一个数都已是分解为几个不重复的素数,设dp[i]为含有因数i的最长序列有多长 然后遍历一下这串数字,每次更新dp[a[i]]=max(dp[j]+1,dp[a[i]]),j表示a[i]的素因数,再将每个素因数更新为 最大值. for(int i = 1 ; i <= n ; i++)

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

http://codeforces.com/problemset/problem/586/C 题意:1~n个孩子排成一排看病.有这么一个模型:孩子听到前面的哭声自信心就会减弱:第i个孩子看病时会发出v[i]的叫声,他后面的那个人的自信心(不是p[i+1])会减少v[i],再后面一个会减少v[i]-1,如此下去直到声音减弱为0.若某个人的自信心小于0,则他哭着跑回家,他身后的所有人会减掉d[i] 的自信. 题解:直接模拟很困难,有一个想法是将逃跑的孩子的声音和将他吓跑的(正在接诊的)声音叠加成s,

CodeForces - 1327A Sum of Odd Integers(数学+思维)

Example input Copy 6 3 1 4 2 10 3 10 2 16 4 16 5 output Copy YES YES NO YES YES NO Note In the first test case, you can represent 3 as 3. In the second test case, the only way to represent 4 is 1+3. In the third test case, you cannot represent 10 as

CodeForces 710B Optimal Point on a Line (数学,求中位数)

题意:给定n个坐标,问你所有点离哪个近距离和最短. 析:中位数啊,很明显. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring>

Codeforces 876B:Divisiblity of Differences(数学)

B. Divisiblity of Differences You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible. Numbers can be repeated in the origi

Codeforces Gym 100513D D. Data Center 前缀和 排序

D. Data Center Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/560/problem/B Description The startup "Booble" has shown explosive growth and now it needs a new data center with the capacity of m petabytes. Booble can b