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 ...
随机推荐
- Entity Framework 学习记录
msdn :https://msdn.microsoft.com/zh-cn/data/ee712907.aspx code first 入门: https://msdn.microsoft.com ...
- 廖雪峰Java13网络编程-1Socket编程-5UDP编程
1. UDP编程: 不需要建立连接 可以直接发送和接收数据 1.1 客户端 DatagramSocket sock = new DatagramSocket(){} sock.connect(addr ...
- python的meshgrid用法和3D库 mpl_toolkits.mplot3d 与PolynomialFeatures多项式库学习
meshgrid import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Ax ...
- 容器安全与EDR的异同
以Docker为代表的容器技术,直接运行于宿主机操作系统内核,因此对于容器安全,很多人会有着这样的疑问:EDR(Endpoint Detection and Response)等主机安全方案,能否直接 ...
- 深入理解JVM(一)类加载器部分、类变量、常量、jvm参数
类加载概述 在java代码中,类型的加载.连接与初始化过程都是在程序运行期间完成的 类型:class.interface(object本身).类型可在运行期间生成,如动态代理.一种runting概念 ...
- 《DSP using MATLAB》Problem 8.4
今天是六一儿童节,陪伴不了家人,心里思念着他们,看着地里金黄的麦子,远处的山,高高的天 代码: %% ------------------------------------------------- ...
- layui -关闭窗口方法
1.关闭当前窗口 var index=parent.layer.getFrameIndex(window.name); //获取当前窗口的nameparent.layer.close(index); ...
- Swagger发布服务器时错误 500 : { "Message": "An error has occurred." }
在做Web API的文档自动生成时,本机调试都正常,发布到服务器上出现500错误 500 : { "Message": "An error has occurred.&q ...
- ls- Linux必学的60个命令
1.作用 ls命令用于显示目录内容,类似DOS下的dir命令,它的使用权限是所有用户. 2.格式 ls [options][filename] 3.options主要参数 -a, --all:不隐藏任 ...
- python基础-迭代器
1.迭代:指的是一个重复的过程,每一次重复称为一次迭代,并且每一次重复的结果 是下一次重复的初始值 2.为什么要有迭代器 对于序列类型:str list tuple 可以依赖索引来迭代取值,但是对于d ...