cdoj第13th校赛初赛H - Hug the princess
http://acm.uestc.edu.cn/#/contest/show/54
H - Hug the princess
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
There is a sequence with n elements. Assuming they are a1,a2,⋯,an.
Please calculate the following expession.
In the expression above, ^ | & is bit operation.
Input
The first line contains a single integer n, which is the size of the sequence.
The second line contains n integers, the ith integer ai is the ith element of the sequence.
1≤n≤100000,0≤ai≤100000000
Output
Print the answer in one line.
Sample input and output
Sample Input | Sample Output |
---|---|
2 |
6 |
思路:首先,自己可以通过举几个例子来验证,异或运算与与运算之和刚好等价于或运算,或者可以这样想,异或是(1,0)、(0,1),与是(1,1),合起来刚好是或。然后题目就是求两倍的或运算了。然后,每一个ai都与aj或运算(i<j),每次ai与aj或的时候,aj二进制位上是1的数位在或运算后总还是1,所以前面有多个ai与aj或,最后结果里就有多少个aj的和;然后考虑aj上是0的数位(比如第4位),记录前面所有的ai中哪些数在这个第4位上是1,记为sum[4],最后的结果里就有sum[4]*(1<<4),因为有权值的嘛。这样O(n)地扫一遍,就行了。如果就像我第一次那样直接O(n*n)地把所有数进行或运算,呵呵,tle了。
这是官方题解:
代码:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector> using namespace std; #define PI acos(-1.0)
#define EPS 1e-10
#define lll __int64
#define ll long long
#define INF 0x7fffffff ll a[];
vector<ll> cnt; int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int n;
ll tn=;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%lld",&a[i]);
tn=max(tn,a[i]);
}
ll ans=,t=;
while(tn){
t++;
tn>>=;
cnt.push_back();
}
for(int i=;i<n;i++){
ans+=a[i]*i;
for(int j=;j<t;j++){
if(!((a[i]>>j)&)){
ans+=(<<j)*cnt[j];
}else{
cnt[j]++;
}
}
}
printf("%lld\n",ans*);
return ;
}
cdoj第13th校赛初赛H - Hug the princess的更多相关文章
- cdoj第13th校赛初赛F - Fabricate equation
http://acm.uestc.edu.cn/#/contest/show/54 F - Fabricate equation Time Limit: 3000/1000MS (Java/Other ...
- cdoj第13th校赛初赛L - Lovely princess
http://acm.uestc.edu.cn/#/contest/show/54 L - Lovely princess Time Limit: 3000/1000MS (Java/Others) ...
- cdoj第13th校赛初赛A - AC Milan VS Juventus 【枚举】
http://acm.uestc.edu.cn/#/contest/show/54 A - AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Oth ...
- 牛客网-湘潭大学校赛重现H题 (线段树 染色问题)
链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 n个桶按顺序排列,我们用1~n给桶标号.有两种操作: 1 l r c 区间[l,r]中的每个桶中 ...
- PKU2018校赛 H题 Safe Upper Bound
http://poj.openjudge.cn/practice/C18H 题目 算平均数用到公式\[\bar{x}=\frac{x_1+x_2+x_3+\cdots+x_n}{n}\] 但如果用in ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- 2014上半年acm总结(1)(入门+校赛)
大一下学期才开始了acm,不得不说有一点迟,但是acm确实使我的生活充实了很多,,不至于像以前一样经常没事干= = 上学期的颓废使我的c语言学的渣的一笔..靠考前突击才基本掌握了语法 寒假突然醒悟, ...
- 2017CUIT校赛-线上赛
2017Pwnhub杯-CUIT校赛 这是CUIT第十三届校赛啦,也是我参加的第一次校赛. 在被虐到崩溃的过程中也学到了一些东西. 这次比赛是从5.27早上十点打到5.28晚上十点,共36小时,中间睡 ...
- HZNU第十二届校赛赛后补题
愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...
随机推荐
- mysql 不同事务隔离级别
repeatable read 在同一事务中,同一查询多次进行时候,由于其他插入操作(insert)的事务提交,导致每次返回不同的结果集. 标准的repeatable read是允许幻读的,因为这一级 ...
- pycharm fiddler requests.exceptions.SSLError
一.SSL问题1.不启用fiddler,直接发https请求,不会有SSL问题(也就是说不想看到SSL问题,关掉fiddler就行) 2.启动fiddler抓包,会出现这个错误:requests.ex ...
- ossfs工具将OSS挂载到阿里云linux系统目录例子
ossfs 是基于 aliyun OSS 的 fuse 客户端了,所以我们把它用在阿里云上肯定是没有错了,下面来看一篇关于ossfs工具将OSS挂载到阿里云linux系统目录例子,具体的细节如下文介绍 ...
- seaborn分类数据可视化
转载:https://cloud.tencent.com/developer/article/1178368 seaborn针对分类型的数据有专门的可视化函数,这些函数可大致分为三种: 分类数据散点图 ...
- 解决WPF两个图片控件显示相同图片因线程占用,其中一个显示不全的问题
在做项目的过程中遇到这样一个问题,下面提出一种解决方法,主要思想是图片的Copy,如还有其他方法,欢迎交流. 在前端图片控件绑定显示时,使用转换器进行转义绑定 (1)转换器: public cla ...
- PreApplicationStartMethodAttribute的使用
先预备一个类,用于Start时调用 public static class MyPreApplicationStart { public static void RegisterGlobalFilte ...
- python 9*9示例
# 9*9 乘法表# def nine_nine():# for i in range(1, 10):# for j in range(1, i+1):# ...
- 开发组件:REST API
REST API 最佳入门指南 https://blog.csdn.net/px01ih8/article/details/78674685
- python连接数据库(pymysql)及数据库加密
内容: 1.pymysql介绍 2.pymysql基本使用 3.数据库加密 参考:http://www.cnblogs.com/wupeiqi/articles/5713330.html 1.pymy ...
- shell $*与$@的区别
[root@bgx shell]# cat tt.sh #! /bin/bash test() { echo "未加引号,二者相同" echo $* echo $@ echo &q ...