codeforces 810 C - Do you want a date?

codeforces 810C - Do you want a date?

題意:

​ 有一個集合,其中有n個數字,這個集合有很多子集,現在規定每一個子集必須至少有兩個數字,其中子集中兩個數字最大的差值就是這個子集的函數值,現在求出所有子集的函數值的和,需要取模。

思路:

​ 很容易想到枚舉集合中最大的值的位置,需要對元素排序,例如在i位置和j位置(i

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 3*1e5 + 10;
typedef long long LL;
const LL mod = 1e9+7;

int n;
LL a[maxn];
LL sum[maxn];
LL two[maxn];

int main()
{
//    freopen("in.txt","r",stdin);

    scanf("%d",&n);
    two[0] = 1;
    for(int i = 1;i <= n; i++) {
        two[i] = two[i-1]*2%mod;
    }
    for(int i = 1;i <= n; i++) {
        scanf("%I64d",&a[i]);
    }
    sort(a+1,a+1+n);
    for(int i = 1;i <= n; i++) {
        sum[i] = (a[i] + sum[i-1])%mod;
    }
    LL ans = 0;
    for(int i = 0;i <= n-2; i++) {
        LL temp = (sum[n]-sum[i+1]-sum[n-i-1]);
        temp %= mod;
        if(temp < 0) {
            temp += mod;
        }
        ans = (ans + two[i]*temp%mod)%mod;
    }
    printf("%I64d\n",ans);

    return 0;
}
發佈了365 篇原創文章 · 獲贊 14 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章