题意:

给定n个不重复的数, 求出这些数的所有子集, 然后设一个数Ni 为 第i个子集中,最大的数 - 最小的数。 然后将i个 Ni求和, 结果mod 1e9 + 7。

分析:

首先将n个数排列,生成一个单调的数列。

举个例子, 如 1 3 5 7 9。

可以看出 1 作为一个子集中最小的数会有 2^4 - 1 = 15种(1 和 3 5 7 9组合, 3 5 7 9任意的非空子集有2^4 - 1种) , 作为最大的数有2^0 - 1 = 0(因为没有数比1小).

同理 9 作为一个子集中最小的数会有2^0 -1= 0 种, 作为最大的数有 2^4 - 1 = 15种。

再例如 3 , 作为最小的数会有 2 ^ 3 - 1  =  7种, 作为最大的数会有2^1 -1 = 1种。

所以我们可以得出以下公式 :

作为最大的种类数就是 pow(2,有多少个数在i的左边) -1

作为最小的种类数就是 pow(2,有多少个数在i右边) -1

a[i] *( i作为最大的数种类 - i作为最小的数的种类),可以求出这个数对答案的影响, 把n个a[i]求出来就是答案。

由于个数会去到很大, 所以可以用快速幂加速.

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5 +;
const int mod = 1e9 + ;
int n;
long long a[maxn];
typedef long long ll;
ll quickmod(ll a, ll b, ll m)
{
ll ans = ;
while (b)//用一个循环从右到左便利b的所有二进制位
{
if (b & )//判断此时b[i]的二进制位是否为1
{
ans = (ans*a) % m;//乘到结果上,这里a是a^(2^i)%m
b--;//把该为变0
}
b /= ;
a = a*a%m;
}
return ans;
}
int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
{
scanf("%lld", &a[i]);
}
sort(a+,a++n);
long long ans = ;
for(int i = ; i < n/+; i++)
{
long long t = ;
t += (a[i] * -(quickmod(,n-i,mod) - quickmod(,i-,mod)));
t += (a[n+-i] * (quickmod(,n-i,mod) - quickmod(,i-,mod)));
t %= mod;
ans = (ans +t) % mod;
}
printf("%lld\n", (ans + mod) % mod );//ans可能为负, 需要+mod
}

Codeforce 810C Do you want a date?的更多相关文章

  1. Codeforces 810C Do you want a date?(数学,前缀和)

    C. Do you want a date? time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  2. Codeforce 515A - Drazil and Date

    Someday, Drazil wanted to go on date with Varda. Drazil and Varda live on Cartesian plane. Drazil's ...

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

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

  4. Two progressions CodeForce 125D 思维题

    An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...

  5. CodeForce 577B Modulo Sum

    You are given a sequence of numbers a1, a2, ..., an, and a number m. Check if it is possible to choo ...

  6. CodeForce 192D Demonstration

    In the capital city of Berland, Bertown, demonstrations are against the recent election of the King ...

  7. CodeForce 176C Playing with Superglue

    Two players play a game. The game is played on a rectangular board with n × m squares. At the beginn ...

  8. CodeForce 222C Reducing Fractions

    To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractio ...

  9. CodeForce 359C Prime Number

    Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1,  ...

随机推荐

  1. 组合数学练习题(一)——Chemist

    题意: 从 n 个人中选出不超过 k 个人,再在选出的人中选出一些人成为队员,再在队员中选一名队长,求不同的方案数.答案 mod 8388608. 共有T组询问,每次给你n和k.T ≤ 10^4 k ...

  2. [转]POJ WA/RE指南

    "POJ上头的题都是数学题",也不知道是那个家伙胡诌的--但是POJ的要求就是算法通过了也不让你AC.下面本人就这560题的经验,浅谈一下WA/RE了怎么办.  以下内容是扯淡-- ...

  3. Qt对象模型之二:对象树与元对象系统

    一.对象树的概念 Qt中使用对象树(object tree)来组织和管理所有的QObject类及其子类的对象.当创建一个QObject时,如果使用了其他的对象作为其父对象(parent),那么这个 Q ...

  4. [POI2008]KUP

    Description 给一个\(n\times n\)的地图,每个格子有一个价格,找一个矩形区域,使其价格总和位于[k,2k] Input 输入k n(n<2000)和一个\(n\times ...

  5. 官方XmlPullParser和网络解析xml示例及详述

    Parsing XML Data This lesson teaches you to Choose a Parser Analyze the Feed Instantiate the Parser ...

  6. Vue自定义过滤器格式化数字三位加一逗号

    <template> <div class="index-compont"> <div class="totalCount"> ...

  7. 227 Basic Calculator II 基本计算器II

    实现一个基本的计算器来计算一个简单的字符串表达式. 字符串表达式仅包含非负整数,+, - ,*,/四种运算符和空格 . 整数除法仅保留整数部分. 你可以假定所给定的表达式总是有效的. 一些例子: &q ...

  8. 框架系列~OwinSelfHost自宿主的使用

    在进入mvc5之后,OWIN变更很主推,很热,关于OWIN的文章也就出来了,下面阅读了dudu和一些园友的文章,自己也做了一个SelfHost的程序,测试了一下,感觉还是比较有Core的风格,可能也是 ...

  9. ZOJ 3605Find the Marble(dp)

    ZOJ 3605 大体意思就是 找出随机选了K个交换后 石子在第i个罐子里的概率最大 也就是可能的总数最大 这样就可以写出递推方程 dp[i][j][k] += dp[i-1][e][k]; (0&l ...

  10. 聊聊mq的使用场景

    mq的作用 通过异步方式对系统解耦 增加系统的并发处理能力 通过异步方式对系统解耦 以用户注册为例,一般情况下: 分下一下,上面过程存在的一些问题: 注册过程会调用4个服务(注册服务.邮件服务.短信服 ...