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 ;
}
最新文章
- Rainyday.js – 使用 JavaScript 实现雨滴效果
- JAVA插入sql代码
- python学习笔记-(五)字符串&;字典
- SILVERLIGHT&#160;多维表头、复杂表头&#160;MULTIPLE&#160;HEADER
- electron小例子
- WPF 自定义窗口标题栏
- 更有效率的使用 Visual Studio - 快捷键
- QT设置前景图位置(配色简单漂亮)
- Oracle 重建索引脚本
- 批处理获取IP地址
- 【转】ethtool 命令详解
- Ubuntu的Redis安装
- GEETEST极验召集互联网大佬齐聚光谷,共同探讨交互安全问题
- python_12 模块
- vue.js插入dom节点的方式
- 关于zynq7 中MIO的理解
- springmvc转换JSON数据
- 关于Python的集合set
- java浅拷贝和深拷贝
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)