【组合 数学】codeforces C. Do you want a date?
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?的更多相关文章
- 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet
题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...
- 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...
- 组合数学题 Codeforces Round #108 (Div. 2) C. Pocket Book
题目传送门 /* 题意:每一次任选i,j行字符串进行任意长度前缀交换,然后不断重复这个过程,问在过程中,第一行字符串不同的个数 组合数学题:每一列不同的字母都有可能到第一行,所以每列的可能值相乘取模就 ...
- 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 ...
- 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 ...
- 数学 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 ...
- 数学 Codeforces Round #308 (Div. 2) B. Vanya and Books
题目传送门 /* 水题:求总数字个数,开long long竟然莫名其妙WA了几次,也没改啥又对了:) */ #include <cstdio> #include <iostream& ...
- 数学 Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun
题目传送门 /* 水题,就是用三点共线的式子来判断射击次数 */ #include <cstdio> #include <cmath> #include <string& ...
- 数学 - 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整除,系统会回答 ...
随机推荐
- UGUI_屏幕适配
引用:http://www.xuanyusong.com/archives/3278#comments 1.可以选择的有三种: 1.Screen Space – overlay 此模式不需要UI摄像 ...
- 前端之HTML样式
<!doctype html> h5的文档声明 <html> 网页的根标签(根元素 html)--所有的代码都放置在此内 <head> <meta chars ...
- AJPFX总结线程创建的两种方法
创建线程的第一种方式:继承Thread ,由子类复写run方法.步骤:1,定义类继承Thread类:2,目的是复写run方法,将要让线程运行的代码都存储到run方法中:3,通过创建Thread类的子类 ...
- Ant题解
Description: 一根长度为L厘米的木棒上有N只蚂蚁,每只蚂蚁要么向左走,要么向右走,速度为1厘米/秒.当两只蚂蚁相撞时,他们会同时掉头(掉头时间不计)给出每只蚂蚁距离木棒左端的距离,问多少秒 ...
- IDEA下MyBatis错误总结
1. Pom.xml配置 语法顺序 <properties resource="config.properties"> </properties> < ...
- APM系列-国外新兴厂商New Relic vs. AppDynamics
前: New Relic的上市使得IT和资本界开始重新重视APM,当然跟传统APM相比,New Relic还是有相当的创新,另外还有一点是目前的创业潮导致的企业级需求增大. In recent yea ...
- 关于php的问题
$polling_items = db_fetch_assoc("SELECT * FROM poller_item WHERE rrd_next_step<=0 ORDER by h ...
- javascript动态添加、修改、删除对象的属性与方法
在其他语言中,对象一旦生成,就不可更改了,要为一个对象添加修改成员必须要在对应的类中修改,并重新实例化,而且程序必须经过重新编译.JavaScript 中却非如此,它提供了灵活的机制来修改对象的行为, ...
- 洛谷 P1548 棋盘问题
题目描述 设有一个N*M方格的棋盘(l<=N<=100,1<=M<=100)(30%) 求出该棋盘中包含有多少个正方形.多少个长方形(不包括正方形). 例如:当 N=2, M= ...
- TCP socket如何判断连接断开
http://blog.csdn.net/zzhongcy/article/details/21992123 SO_KEEPALIVE是系统底层的机制,用于系统维护每一个tcp连接的. 心跳线程属于应 ...