Hackerrank--XOR love (Math系列)
Devendra loves the XOR operation very much which is denoted by ∧ sign in most of the programming languages. He has a list A of N numbers and he wants to know the answers of M queries. Each query will be denoted by three numbers i.e. K,P,R .
For query K,P and R, he has to print the value of the KPRsum which can be described as given below. As the value of the KPRsum can be large. So, print it modulus(109+7).
KPRsum=∑i=PR−1∑j=i+1R(K⊕(A[i]⊕A[j]))Input Format
The first line contains an integer N, i.e., the number of the elements in the list. List is numbered from 1 to N.
Next line will contain N space seperated integers.
Third line will contain a number M i.e. number of queries followed by M lines each containing integers K,P&R.Output Format
Print M lines, ith line will be answer of ith query. Answer will be 0 in case of P=R.Constraints
1≤N≤105
1≤A[i]≤106
1≤M≤105
0≤K≤106
1≤P≤R≤NSample Input
3
1 2 3
2
1 1 3
2 1 3
Sample Output
5
4
Explanation
For first query, it will will be
(1⊕(1⊕2))+(1⊕(1⊕3))+(1⊕(2⊕3))=5
话说这题又让我想起了去年南京赛区的一道题目,题目难度属于中等,不过当时是没有想出来。
这题算是那个题目的简单版吧,因为每次只需要选两个数。
题目意思就是:给出一个数列,有m个询问,每次询问给出三个数,k, p, r。
求从数组的区间[p, r]内任意选出两个数a[i], a[j], (i > j),每次得到一个数:k^a[i]^a[j],将其进行累加.
输出最终的和。
Accepted Code:
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + ;
const int MAX_N = ;
int a[MAX_N], d[][MAX_N];
int n, m, k, p, r; int main(void) {
ios::sync_with_stdio(false);
cin >> n;
for (int i = ; i <= n; i++) cin >> a[i]; for (int i = ; i < ; i++) {
d[i][] = ;
for (int j = ; j <= n; j++) {
d[i][j] = d[i][j - ] + ((a[j]&(<<i)) ? : );
}
} cin >> m;
while (m--) {
cin >> k >> p >> r;
LL ans = ;
for (int i = ; i < ; i++) {
LL ones = d[i][r] - d[i][p - ];
LL zeros = r - p + - ones;
if ( == (k & (<<i))) ans = (ans + ones * zeros * (<<i)) % MOD;
else ans = (ans + (ones * (ones - ) + zeros * (zeros - )) / * (<<i)) % MOD;
}
cout << ans << endl;
}
return ;
}
Hackerrank--XOR love (Math系列)的更多相关文章
- # DZY Love Math 系列
DZY Love Math 系列 [BOZJ3309] DZY Loves Math 顺着套路就能得到:\(Ans = \sum_{T=1}\lfloor \frac{n}{T} \rfloor \l ...
- PHP 基础篇 - PHP 的 BC MATH 系列数学函数
一.常见问题 用 PHP 做计算时经常会遇到精度带来的问题,下面来看两个常见的例子: 1. 运算比较 下面表达式输出的结果不是相等: <?php echo 2.01 - 0.01 == 2 ? ...
- DZY Loves Math 系列详细题解
BZOJ 3309: DZY Loves Math I 题意 \(f(n)\) 为 \(n\) 幂指数的最大值. \[ \sum_{i = 1}^{a} \sum_{j = 1}^{b} f(\gcd ...
- DZY Loves Math系列
link 好久没写数学题了,再这样下去吃枣药丸啊. 找一套应该还比较有意思的数学题来做. [bzoj3309]DZY Loves Math 简单推一下. \[\sum_{i=1}^n\sum_{j=1 ...
- [BZOJ] DZY Loves Math 系列 I && II
为了让自己看起来有点事干 ,做个套题吧..不然老是东翻翻西翻翻也不知道在干嘛... \(\bf 3309: DZY \ Loves \ Math\) 令 \(h=f*\mu\) 很明显题目要求的就是\ ...
- BZOJ DZY Loves Math系列
⑤(BZOJ 3560) $\Sigma_{i_1|a_1}\Sigma_{i_2|a_2}\Sigma_{i_3|a_3}\Sigma_{i_4|a_4}...\Sigma_{i_n|a_n}\ph ...
- 走进javascript——类型
ECMAScript语言类型对应于使用ECMAScript语言的ECMAScript程序员直接操作的值.ECMAScript语言类型有以下几种Undefined,Null,Boolean,String ...
- JVM-2.Class文件结构
1.Class文件 (1)无关性:除了平台无关性,JVM还支持语言无关性:目前Clojure.Groovy.JRuby.Jyphon.Scala等语言可以在JVM上运行.实现语言无关性的原理仍然是字节 ...
- 【Machine Learning】决策树之简介(1)
Content 1.decision tree representation 2.ID3:a top down learning algorithm 3.expressiveness of data ...
随机推荐
- DMZ在虚拟化环境中的部署
常见的方法有三种: 1.分别部署 2.部分虚拟化 3.全部虚拟化 传统DMZ部署结构: 分别部署: 想要保持DMZ区域物理隔离采用这种方法,每个区域分别部署进入不同的服务器集群,区域之间的连接采用物理 ...
- 非旋Treap及其可持久化
平衡树这种东西,我只会splay.splay比较好理解,并且好打,操作方便. 我以前学过SBT,但并不是很理解,所以就忘了怎么打了. 许多用平衡树的问题其实可以用线段树来解决,我们真正打平衡树的时候一 ...
- 介绍一下再Apache下的Tomcat负载均衡的一些使用问题
在负载均衡技术中,硬件设备是比较昂贵的,对于负载均衡的学习者如果不是在企业中应用或者是学员中学习,很少有机会能碰到实际操作的训练.(http://xz.8682222.com)所以,很多朋友都会选择软 ...
- 开发函数计算的正确姿势 —— 使用 ROS 进行资源编排
前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...
- thinkphp Mongo模型
Mongo模型是专门为Mongo数据库驱动而支持的Model扩展,如果需要操作Mongo数据库的话,自定义的模型类必须继承Think\Model\MongoModel. Mongo模型为操作Mongo ...
- DRF 请求生命周期以及各模块解析
目录 rest_framework框架的封装特点 原生Django与DRF比较 APIView 的请求生命周期 请求模块(request) 解析模块(parser_classes) 异常模块(exce ...
- PAT甲级——A1105 Spiral Matrix【25】
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- Python ----键抠图
背景 这段时间,经常有人来找我,说我是学计算机的,能不能帮他p一下证件照,我只想说,MMP的,我是学计算机的不错,可我不会ps阿. 我想了一会,python 这么火,能不能来个自动抠图,说好就干吧 介 ...
- hibernate hql语句 注意事项
现在有实体类 Student 和User . public class Student{ private String id; private Sting classRoom; private Use ...
- 基于neighborhood models(item-based) 的个性化推荐系统
文章主要介绍的是koren 08年发的论文[1], 2.2neighborhood models部分内容(其余部分会陆续补充上来). koren论文中用到netflix 数据集, 过于大, 在普通的 ...