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, ...
随机推荐
- zabbix离线安装
LAMP环境 1.apache安装 #安装包(yum install --downloadonly --downloaddir=/opt/apache httpd httpd-devel) 1.1拷贝 ...
- c语言程序设计案例教程(第2版)笔记(四)—指针、分配存储空间、文件
零散知识点: 指针类型:每个变量占用的首单元地址称为这个变量的存储地址. “&”为“取地址运算符”.格式:& 变量名.功能:返回指定变量的存储地址. “*”为“取内容运算”.格式:* ...
- [POJ1721]Cards
Description 剀剀和凡凡有N张牌(依次标号为1,2,--,N)和一台洗牌机.假设N是奇数.洗牌机的功能是进行如下的操作:对所有位置I(1≤I≤N),如果位置I上的牌是J,而且位置J上的牌是K ...
- 用for循环实现的菱形图案
package com.wh.lingxing; import java.util.Scanner; public class LingXing { public static void main(S ...
- 转 SQL*PLUS中的替换变量(& &&)
今天oracle support提供一个脚本,大致如下: PROMPT ROUTING_SEQUENCE_ID = &ROUT_SEQ_ID PROMPT OPERATION_SEQUENCE ...
- 转 Oracle Transportable TableSpace(TTS) 传输表空间 说明
############1 迁移数据库的集中方法 三.相关技术 迁移方式 优势 不足1 Export and import • 对数据库版本,以及系统平台没有要求 • 不支持并发,速度慢• 停机时 ...
- 435 Non-overlapping Intervals 无重叠区间
给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠.注意: 可以认为区间的终点总是大于它的起点. 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠.示例 ...
- 微信里去掉下拉select的边框
<select name="gender" id="" class=" " style=" -webkit-appeara ...
- 移动端展示pdf(在线打开pdf)
需求:在手机微信浏览器或者其他浏览器中打开pdf 准备:前端插件:查找pdf.js 官网地址:http://mozilla.github.io/pdf.js/ 在官网中下载demo 注释:pdf的d ...
- 掌握Spark机器学习库-08.2-朴素贝叶斯算法
数据集 iris.data 数据集概览 代码 import org.apache.spark.SparkConf import org.apache.spark.ml.classification.{ ...