题意:

给定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. 搜索刷题记录by cellur925

    我好菜啊!连暴搜都不会! 注意边界退出! 特开此帖,记录搜索学习之路!(逃) 1.全排列 2.八皇后 3.数的划分 由于此题有同一划分方法算一个的限制,我们为了避免搜多,可以使搜出的结果满足单调不降性 ...

  2. #undef及其用法

    简    介 在后面取消以前定义的宏定义 在此程序中,我们将取消在先前程序中对预处理器的定义. #include <stdio.h> int main( void ) { #define ...

  3. ubuntu下进入xampp mysql命令行

    在命令行下进入到/opt/lampp/bin目录,使用命令:sudo ./mysql,回车即可. 注意:运行这个命令需要加上sudo,以root权限来运行,不然有些数据库没有权限查看.

  4. Linux oraenv Tips

    Linux for the Oracle DBA -Customizing the Oracle User's Environment There are many ways to customize ...

  5. javaweb-JSP action中附件下载的写法

     附件下载(包括图片,exl,word)在前台必须给出一个iframe 或者类似于window的窗口,另外,Java文件下载不能通过ajax进行请求,必须添加src属性首选,前台的链接拼接html如下 ...

  6. @GetMapping和@PostMapping 和@RequestMapping区别

    @GetMapping 用于将HTTP GET请求映射到特定处理程序方法的注释. 具体来说,@GetMapping是一个作为快捷方式的组合注释@RequestMapping(method = Requ ...

  7. [Tunny]Grunt基础介绍

    [黄映焜/Tunny,20140711] Grunt是一个JavaScript任务管理器,对于需要反复重复的任务,例如压缩.编译.单元测试.代码检查等,自动化工具可以减轻你的劳动,简化你的工作. 本文 ...

  8. 详解java基础--抽象类、接口与多态

    抽象类.接口.多态都是面向对象中很基础的东西,我相信看到能本篇博客的人本不会再纠结它的基本定义了,本篇文章将尽量的更加深层次的挖掘其内涵,希望能对大家有帮助. 一.抽象类 1.形式 abstract ...

  9. vscode前端开发软件配搭好用的插件

    使用方法,可以在官网中搜索需要的插件或者在VsCode的“”扩展“”中搜索需要的插件添加方法使用Ctrl+P, 输入 ext install xxxx ,搜索要安装的插件,点击安装按钮即可(各取所需插 ...

  10. C++学习之继承篇

    今天通过对实验二继承,重载,覆盖的学习,让我更深一步理解了这些概念的区别. 首先来明确一个概念,函数名即地址,也就是说函数名就是个指针. 编译阶段,编译器为每个函数的代码分配一个地址空间并编译函数代码 ...