codeforces.com/contest/810/problem/C

【题意】

给定一个集合A,求

输入:

【思路】

基数为n的集合有2^n-1个非空子集。

首先n个数要从小到大排序,枚举最后的集合中最大和最小的元素是a[i]和a[j](i< j); 
则f值是a[j]-a[i]的集合有2^(j-i-1)个; 然后答案+=(a[j]−a[i])*2^(j-i-1);

这样时间复杂度是O(n^2)。

我们可以把各项组合一下,把2^i提取出来。

比如考虑系数为 
2^1的 
必然是 
a[3]-a[1] 
a[4]-a[2] 
a[5]-a[3] 
… 
a[n]-a[n-2] 
=sum[n]-sum[i+1]-sum[n-i-1];

sum[i]是a[i]的前缀和。

这样对于[0,n-2]枚举2^i,时间复杂度是O(n),加上排序,总的时间复杂度是O(nlogn)

【注意】

sum[i]是取余后的结果,所以sum[n]-sum[i+1]-sum[n-i-1]可能是负数,要(+mod)%mod

【Accepted】

 #include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=3e5+;
const ll mod=1e9+;
int a[maxn];
int n;
ll sum[maxn];
ll two[maxn]; void Init()
{
two[]=;
for(int i=;i<=maxn;i++)
{
two[i]=(two[i-]*)%mod;
}
}
int main()
{
Init();
while(~scanf("%d",&n))
{
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
sort(a+,a+n+);
for(int i=;i<=n;i++)
{ sum[i]=(sum[i-]+(ll)a[i])%mod;
}
ll ans=;
for(int i=;i<=n-;i++)
{
ans=(ans+two[i]*(sum[n]-sum[n-i-]-sum[i+]+mod)%mod)%mod;
}
cout<<ans<<endl;
}
return ;
}

【组合 数学】codeforces C. Do you want a date?的更多相关文章

  1. 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet

    题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...

  2. 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun

    题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...

  3. 组合数学题 Codeforces Round #108 (Div. 2) C. Pocket Book

    题目传送门 /* 题意:每一次任选i,j行字符串进行任意长度前缀交换,然后不断重复这个过程,问在过程中,第一行字符串不同的个数 组合数学题:每一列不同的字母都有可能到第一行,所以每列的可能值相乘取模就 ...

  4. 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 ...

  5. ACM选修hust 1075 组合+数学+期望值

    Description Input Output Sample Input 2 2 1 0 1 1 0 3 1 0 1 1 1 0 1 1 1 0 Sample Output 0.500 1.125 ...

  6. 数学 Codeforces Round #282 (Div. 2) B. Modular Equations

    题目传送门 题意:a % x == b,求符合条件的x有几个 数学:等式转换为:a == nx + b,那么设k = nx = a - b,易得k的约数(>b)的都符合条件,比如a=25 b=1 ...

  7. 数学 Codeforces Round #308 (Div. 2) B. Vanya and Books

    题目传送门 /* 水题:求总数字个数,开long long竟然莫名其妙WA了几次,也没改啥又对了:) */ #include <cstdio> #include <iostream& ...

  8. 数学 Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun

    题目传送门 /* 水题,就是用三点共线的式子来判断射击次数 */ #include <cstdio> #include <cmath> #include <string& ...

  9. 数学 - Codeforces Round #319 (Div. 1)A. Vasya and Petya's Game

    Vasya and Petya's Game Problem's Link Mean: 给定一个n,系统随机选定了一个数x,(1<=x<=n). 你可以询问系统x是否能被y整除,系统会回答 ...

随机推荐

  1. 单页Html及Android App供小孩学习常用汉字

    为了检验及帮助小孩学习常用汉字,简单开发本网页应用: 常用汉字是按使用频率排序的,来源于网上: 该简单应用 有Android APP下载 “学习常用汉字_20150910.apk” 单页Html 示例 ...

  2. 前端之HTML样式

    <!doctype html> h5的文档声明 <html> 网页的根标签(根元素 html)--所有的代码都放置在此内 <head> <meta chars ...

  3. [转]Android TCP长连接 心跳机制及实现

    背景知识 智能手机上的长连接心跳和在Internet上的长连接心跳有什么不同 Android系统的推送和iOS的推送有什么区别 几种推送的实现方式 协议 1XMPP简介 2 MQTT简介 3移动端消息 ...

  4. jQuery选择器之可见性选择器

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  5. es6语法错误

    哇,今天折腾了好久解决了一个问题,记录一下. 错误: 解决方法:配置babel,将es6语法转换成es5语法 1. 全局安装babel: npm install babel-cli -g 2. 本地安 ...

  6. JDBC性能优化篇

    系统性能. 少用Metadata方法     与其它的JDBC方法相比, 由ResultSet对象生成的metadata对象的相对来说是很慢的. 应用程序应该缓存从ResultSet返回的metada ...

  7. H3C S5024P交换机 vlan实验

    H3C S5024P交换机第二次vlan实验 实验1 与交换机端口G0/1和G0/2相连的PC1与PC2属于VLAN 1,与G0/3和G0/4相连的PC3和PC4属于VLAN 2,PC1.PC2.PC ...

  8. TCP socket如何判断连接断开

    http://blog.csdn.net/zzhongcy/article/details/21992123 SO_KEEPALIVE是系统底层的机制,用于系统维护每一个tcp连接的. 心跳线程属于应 ...

  9. vue 高度 动态更新计算 calcHeight watch $route

    vue 高度 动态更新计算 calcHeight () { // this.tableHeight = window.innerHeight - 210 } }, mounted () { // co ...

  10. 【搜索】P1032 字串变换

    题目描述 已知有两个字串A,B及一组字串变换的规则(至多6个规则): A1​ ->B1​ A2​ -> B2​ 规则的含义为:在 A中的子串 A1​ 可以变换为B1​,A2​ 可以变换为  ...