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, ...
随机推荐
- Miller&&Pollard HDOJ 4344 Mark the Rope
题目传送门 题意:一个长为n(n<2^63)的管子,在管子上做标记,每隔L个长度单位做一个标记,从管子头端开始,保证最后一次标记恰好在管子的尾端.让你找出有多少个这样的L(L<n),且他们 ...
- 题解报告:hdu 1171 Big Event in HDU(多重背包)
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- 223 Rectangle Area 矩形面积
在二维平面上计算出两个由直线构成的矩形叠加覆盖后的面积. 假设面积不会超出int的范围. 详见:https://leetcode.com/problems/rectangle-area/descrip ...
- 窗体WINFORM
窗体的事件:删除事件:先将事件页面里面的挂好的事件删除,再删后台代码里面的事件 Panel是一个容器 1.Label -- 文本显示工具Text:显示的文字取值.赋值:lable1.Text 2.Te ...
- 锁 Lock、重入锁、写入锁
ReentrantLock 重入锁 类似于synchronize 区别与写法上,在需要进行同步的代码部分加上锁定,但不要忘记最后一定要释放锁定, 不然会造成锁永远无法释放,其他线程永远进不来的结果.e ...
- Program received signal SIGILL, Illegal instruction
Program received signal SIGILL, Illegal instruction 这个错误,发现是直接在printf 的%s中直接使用string类型,而没有使用c字符串格式造成 ...
- 提交应用 Windows Phone的应用程序认证要求
本文介绍了 Windows Phone 应用程序或游戏要通过认证并在 Windows Phone Marketplace 中发布而必须满足的策略和技术要求. 1.0 计划概述 设计认证过程的一个核心原 ...
- 为什么jfinal的控制器不用单例模式
先假controller定采用单例模式,通常两种设计方式来存放 HttpServletRequest.HttpServletResponse 等对象,一是利用一个类似于 ActionContext 的 ...
- centOS linux 下PHP编译安装详解
一.下载PHP源码包 wget http://php.net/distributions/php-5.6.3.tar.gz 二.添加依赖应用 yum install -y gcc gcc-c++ ...
- xamarin 学习笔记02- IOS Simulator for windows 安装
微软发布了在window下的ios模拟器 下载 ios模拟器 并安装在windows系统上. Xamarin for Visual Studio 和 网络上的 Mac 中的 Xamarin.iOS 开 ...