题目链接

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≤N

Sample 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系列)的更多相关文章

  1. # DZY Love Math 系列

    DZY Love Math 系列 [BOZJ3309] DZY Loves Math 顺着套路就能得到:\(Ans = \sum_{T=1}\lfloor \frac{n}{T} \rfloor \l ...

  2. PHP 基础篇 - PHP 的 BC MATH 系列数学函数

    一.常见问题 用 PHP 做计算时经常会遇到精度带来的问题,下面来看两个常见的例子: 1. 运算比较 下面表达式输出的结果不是相等: <?php echo 2.01 - 0.01 == 2 ? ...

  3. DZY Loves Math 系列详细题解

    BZOJ 3309: DZY Loves Math I 题意 \(f(n)\) 为 \(n\) 幂指数的最大值. \[ \sum_{i = 1}^{a} \sum_{j = 1}^{b} f(\gcd ...

  4. DZY Loves Math系列

    link 好久没写数学题了,再这样下去吃枣药丸啊. 找一套应该还比较有意思的数学题来做. [bzoj3309]DZY Loves Math 简单推一下. \[\sum_{i=1}^n\sum_{j=1 ...

  5. [BZOJ] DZY Loves Math 系列 I && II

    为了让自己看起来有点事干 ,做个套题吧..不然老是东翻翻西翻翻也不知道在干嘛... \(\bf 3309: DZY \ Loves \ Math\) 令 \(h=f*\mu\) 很明显题目要求的就是\ ...

  6. 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 ...

  7. 走进javascript——类型

    ECMAScript语言类型对应于使用ECMAScript语言的ECMAScript程序员直接操作的值.ECMAScript语言类型有以下几种Undefined,Null,Boolean,String ...

  8. JVM-2.Class文件结构

    1.Class文件 (1)无关性:除了平台无关性,JVM还支持语言无关性:目前Clojure.Groovy.JRuby.Jyphon.Scala等语言可以在JVM上运行.实现语言无关性的原理仍然是字节 ...

  9. 【Machine Learning】决策树之简介(1)

    Content 1.decision tree representation 2.ID3:a top down learning algorithm 3.expressiveness of data ...

随机推荐

  1. C++ 系列:基础知识储备

    Copyright © 2000-2017, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ ----------------- ...

  2. react中使用屏保

    1,默认路由路径为屏保组件 <HashRouter history={hashHistory}> <Switch> <Route exact path="/&q ...

  3. 【JZOJ3400】旅行

    description 从前有一位旅者,他想要游遍天下所有的景点.这一天他来到了一个神奇的王国:在这片土地上,有n个城市,从1到n进行编号.王国中有m条道路,第i条道路连接着两个城市ai,bi,由于年 ...

  4. Java参数校验工具validation实践

    介绍 在项目开发当中,数据校验是你必须要考虑和面对的事情,为此要写上一大串的代码进行校验,这样就会导致代码冗余和一些管理的问题. 例如下面的代码: public void push(List<L ...

  5. 基于MFC的实时可视化项目中视图刷新消息的严谨使用

    在实时可视项目中,视图的实时刷新显示对软件的体验感来说非常重要,当算法的效率达到实时,比如一秒40帧,如果实时显示帧率更不上,则体验感将大大折扣,让用户感觉你的算法并没有40帧,当然最关键的是解决显示 ...

  6. hibernate 映射总结

    单向一对多实体配置:在一的实体中设置多的一方SET集合配置文件:在一的一方用set 设置 one to many表配置:多方表的外键指向一方表的主键; 双向一对多实体配置:在一的实体中设置多的一方SE ...

  7. IDEA本地SBT项目上传到SVN

    需求 将本地创建的一个项目上到SVN 网上很多从SVN下载到idea,提交.更新.删除等操作. 但是少有从本地上传一个项目到svn管理的案例 本文参考https://blog.csdn.net/cao ...

  8. 《DSP using MATLAB》Problem 8.21

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  9. 用DataTable填充实体类List

    /// <summary> /// 用DataTable填充实体类List /// </summary> public static List<T> FillLis ...

  10. np一些基本操作2

    import numpy as nparr1 = np.arange(32).reshape(8,4)print(arr1)arr1 = arr1.reshape(-1);print(arr1)arr ...