Codeforce 810C Do you want a date?
题意:
给定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?的更多相关文章
- 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 ...
- Codeforce 515A - Drazil and Date
Someday, Drazil wanted to go on date with Varda. Drazil and Varda live on Cartesian plane. Drazil's ...
- 【codeforces 810C】Do you want a date?
[题目链接]:http://codeforces.com/contest/810/problem/C [题意] 给你一个集合,它包含a[1],a[2]..a[n]这n个整数 让你求出这个集合的所有子集 ...
- Two progressions CodeForce 125D 思维题
An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...
- 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 ...
- CodeForce 192D Demonstration
In the capital city of Berland, Bertown, demonstrations are against the recent election of the King ...
- 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 ...
- CodeForce 222C Reducing Fractions
To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractio ...
- CodeForce 359C Prime Number
Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1, ...
随机推荐
- A joke about regular expression
As the old computer science joke goes: “Let’s say you have a problem, andyou decide to solve it with ...
- 依赖注入(二)Autofac简单使用
Autofac简单使用 源码下载传上源码,终于学会传文件了. 首先 还是那句话:“不要信我,否则你死得很惨!”. C#常见的依赖注入容器 IoC in .NET part 1: Autofac IoC ...
- c语言程序设计案例教程(第2版)笔记(六)—字符串处理实例
字符串处理 功能描述:从键盘输入一个文本行后,为用户提供菜单选择,实现字符串一些操作——显示文本行.查找并替换指定子串.删除指定子串.统计指定子串数目. 实现代码: #include<stdio ...
- Kruskal算法 分类: c/c++ 算法 2014-10-01 17:09 540人阅读 评论(0) 收藏
Kruskal算法计算最小生成树,只与边有关,时间复杂度O(eloge) 步骤: 1.将边按权值递增排序 2.依次取出边加入最小生成树中并保证无环,判断是否成环可利用并查集. 例:http://ac. ...
- 转 ORA-12638: 身份证明检索失败
ORA-12638: 身份证明检索失败 的解决办法 2008年06月25日 星期三 11:42 the NTS option makes the Oracle client attempt to us ...
- 自定义button上传按钮
<div class="upload_files"> <input type="file" class="upload_icon&q ...
- jQuery 几款比较棒的插件
jQuery滚动监听插件Waypoints 博客分类: Javascript /Jquery / Bootstrap / Web 你是否希望当用户仅仅滚动滑条的时候,就能触发各种各样的动态效果呢? ...
- STM32编程环境配置(kile5)
2018-08-2513:53:33 折腾了很久,花了两天的空闲时间终于烧进去程序了.完成了kile5对stm32编程的环境配置. 1.下载kile5 激活破解 2.安装stm32配置环境 3.加载工 ...
- R Programming week1-Reading Data
Reading Data There are a few principal functions reading data into R. read.table, read.csv, for read ...
- Javascript IE 内存释放
一个内存释放的实例 <SCRIPT LANGUAGE="JavaScript"><!--strTest = "1";for ( var i = ...