【组合 数学】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整除,系统会回答 ...
随机推荐
- P2044 随机数生成器
链接:https://www.luogu.org/problem/show?pid=2044#sub 题目描述 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linea ...
- 解决IDEA Tomcat控制台乱码问题
1.在Tomcat Server的配置中添加一句命令: 神秘代码: -Dfile.encoding=UTF-8 重启Tomcat,ok. 如果还不行,则需要: 1.在Settings中修改文件编码 2 ...
- f# mathprovider
http://mathprovider.codeplex.com/ http://mathnetnumerics.codeplex.com/releases/view/110750 http://py ...
- jq获取设置选中值
var standard = $('input[name="standard"]:checked').val(); $("input[name='advertByid'] ...
- innerHTML的性能问题
看到一遍文章When innerHTML isn’t Fast Enough,反应了innerHTML在操作量大了以后的性能下降的问题. 并且给出了一个replaceHTML的函数 function ...
- 固定table表头
<style> #box{ height:214px; width:500px; overflow-y:auto;/** 必须,否则当表格数据过多时,不会产生滚动条,而是自动延长该div的 ...
- JavaScript-基础类型和运算符
JavaScript-基础类型和运算符 P02.稍微了解 1.js代码需要编写到script标签中 <script type="text/javascript"> 此处 ...
- hql语法001
1. import java.util.List; import org.hibernate.*; import org.junit.Test; import cn.jbit.hibernatedem ...
- ping ip
def ip_and_time(): """ get ip to ping from ip.txt then return two list , each ip that ...
- Spring框架 (log4j :WARN No appenders could be found for logger log4j:WARN Please initialize the log4j system properly.)问题解决
Spring框架需要的jar包 1.Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.baidu.com/s/ ...