【数学】MORE XOR

Given a sequence of nnn numbers a1,a2,⋯ ,ana_1, a_2,\cdots, a_na1,a2,,an and three functions.

Define a function f(l,r)f(l,r)f(l,r) which returns ⊕a[x](l≤x≤r)\oplus a[x] (l \le x \le r)a[x](lxr). The \oplus⊕ represents exclusive OR.

Define a function g(l,r)g(l,r)g(l,r) which returns ⊕f(x,y)(l≤x≤y≤r)⊕f(x,y)\oplus f(x,y)(l \le x \le y \le r)⊕f(x,y)f(x,y)(lxyr)f(x,y).

Define a function w(l,r)w(l,r)w(l,r) which returns ⊕g(x,y)(l≤x≤y≤r)⊕g(x,y)\oplus g(x,y)(l \le x \le y \le r)⊕g(x,y)g(x,y)(lxyr)g(x,y).

You are also given a number of xor-queries. A xor-query is a pair (i,j)(1≤i≤j≤n)(i,j) (1 \le i \le j \le n)(i,j)(1ijn). For each xor-query (i,j)(i, j)(i,j), you have to answer the result of function w(l,r)w(l,r)w(l,r).

Input
Line 111: t(1≤t≤20)t (1 \le t \le 20)t(1t20).

For each test case:

Line 111: n(1≤n≤100000)n (1 \le n \le 100000)n(1n100000).

Line 222: nnn numbers a1,a2,⋯ ,an(1≤ai≤109)a_1, a_2, \cdots, a_n (1 \le a_i \le 10^9)a1,a2,,an(1ai109)

Line 333: q(1≤q≤100000)q (1 \le q \le 100000)q(1q100000), the number of xor-queries.

In the next q lines, each line contains 222 numbers i,ji, ji,j representing a xor-query (1≤i≤j≤n)(1 \le i \le j \le n)(1ijn).

It is guaranteed that sum of nnn and q≤106q \le 10^6q106

Output
For each xor-query (i,j)(i, j)(i,j), print the result of function w(i,j)w(i,j)w(i,j) in a single line.

样例输入
1
5
1 2 3 4 5
5
1 3
1 5
1 4
4 5
3 5
样例输出
2
4
0
1
4

解题思路:
emmmm,我这道题是通过打表寻找规律来解答的。。。
打表的代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int arr[100];
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endiffreopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);for(int l1=1,r1=1;r1<=50;r1++) {memset(arr,0,sizeof arr);for(int l2=l1;l2<=r1;l2++) {for(int r2=l2;r2<=r1;r2++) {for(int l3=l2;l3<=r2;l3++) {for(int r3=l3;r3<=r2;r3++) {for(int k=l3;k<=r3;k++) {arr[k]++;}}}}}//输出的1代表最后的答案这项存在,0代表不存在for(int i=1;i<=r1;i++) {if(arr[i]%2==0) printf("0 ");else printf("1 ");}printf("\n");//例如假设区间为[l,r],第一行代表当区间长度为1时,其答案就是a[l]//当区间长度为2时,其答案为a[l]^a[r]//当区间长度为3时,其答案为a[l+1]//当区间长度为4时,其答案为0}return 0;
}

通过表格我们可以发现,
(r−l+1)%4=0(r-l+1) \%4=0(rl+1)%4=0时,答案为000
(r−l+1)%4=1(r-l+1) \%4=1(rl+1)%4=1时,答案为a[l]⊕a[l+4]⊕a[l+8]⋯a[r]a[l]\oplus a[l+4] \oplus a[l+8] \cdots a[r]a[l]a[l+4]a[l+8]a[r]
(r−l+1)%4=2(r-l+1) \%4=2(rl+1)%4=2时,答案为a[l]⊕a[l+1]⊕a[l+4]⊕a[l+5]⋯a[r−1]⊕a[r]a[l]\oplus a[l+1] \oplus a[l+4] \oplus a[l+5] \cdots a[r-1] \oplus a[r]a[l]a[l+1]a[l+4]a[l+5]a[r1]a[r]
(r−l+1)%4=3(r-l+1) \%4=3(rl+1)%4=3时,答案为a[l+1]⊕a[l+5]⊕a[l+9]⋯a[r−1]a[l+1]\oplus a[l+5] \oplus a[l+9] \cdots a[r-1]a[l+1]a[l+5]a[l+9]a[r1]
因为有10610^6106组查询,所以还需要求一个间隔为444的前缀异或和,这样就可以实现O(1)O(1)O(1)查询
代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int arr[100100];
int sum[4][100100];
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;scanf("%d",&t);while(t--) {int n;scanf("%d",&n);rep(i,1,n) {scanf("%d",&arr[i]);sum[0][i]=sum[1][i]=sum[2][i]=sum[3][i]=0;}sum[1][1]=arr[1];sum[2][2]=arr[2];sum[3][3]=arr[3];sum[0][4]=arr[4];rep(i,5,n) {sum[1][i]=sum[1][i-4]^arr[i];sum[2][i+1]=sum[2][i+1-4]^arr[i+1];sum[3][i+2]=sum[3][i+2-4]^arr[i+2];sum[0][i+3]=sum[0][i+3-4]^arr[i+3];}int q;scanf("%d",&q);rep(i,1,q) {int l,r;scanf("%d%d",&l,&r);int len=r-l+1,ans;if(len%4==0) printf("0\n");else if(len%4==1) {ans=sum[r%4][r]^sum[l%4][l]^arr[l];printf("%d\n",ans);}else if(len%4==2) {ans=sum[r%4][r]^sum[(l+1)%4][l+1]^arr[l+1];ans=ans^(sum[(r-1)%4][r-1]^sum[l%4][l]^arr[l]);printf("%d\n",ans);}else {ans=sum[(r-1)%4][r-1]^sum[(l+1)%4][l+1]^arr[l+1];printf("%d\n",ans);}}}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536294.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【数学】Element Swapping

Element Swapping Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid has an integer sequence a1,a2,a3,…,ana_1,a_2,a_3,\dots,a_na1​,a2​,a3​,…,an​ and he likes it very much. Unfortunately, his naughty roommate BaoBao swapped two elements aia_iai​ an…

【二分+二维前缀和】Largest Allowed Area

Largest Allowed Area 时间限制: 1 Sec 内存限制: 128 MB 提交: 146 解决: 54 [提交] [状态] [命题人:admin] 题目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【数学】Floating-Point Hazard

Floating-Point Hazard 时间限制: 1 Sec 内存限制: 128 MB 提交: 106 解决: 42 [提交] [状态] [命题人:admin] 题目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【manacher】Strings in the Pocket

Strings in the Pocket Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found two strings ss1s2…snss_1s_2\dots s_nss1​s2​…sn​ and tt1t2…tntt_1t_2\dots t_ntt1​t2​…tn​ in his left pocket, where sis_isi​ indicates the iii-th character in…

【并查集+dp】Team

Team 时间限制: 1 Sec 内存限制: 128 MB 提交: 124 解决: 10 [提交] [状态] [命题人:admin] 题目描述 ACM-ICPC is a interesting game. A team takes part in this game must consist of exactly (no more and no less) three players. Every year, many new members wil…

【线段树】Segment Tree

Segment Tree 时间限制: 1 Sec 内存限制: 512 MB 提交: 107 解决: 23 [提交] [状态] [命题人:admin] 题目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach w…

UVA1025——A Spy in the Metro【dp】

题目链接&#xff1a;https://cn.vjudge.net/problem/UVA-1025 题目大意&#xff1a;Mario从第1站出发&#xff0c;目的是在时刻T会见车站 nnn 的一个间谍。由于在车站等待容易被抓&#xff0c;所以应尽量躲在开动的火车上&#xff0c;即在车站等待的时间最短&#xff0c;且Ma…

HDU1284——钱币兑换问题【dp】

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14528 Accepted Submission(s): 8784 Problem Description 在一个国家仅有1分&#xff0c;2分&#xff0c;3分硬币&#xff0c;将钱N兑换成硬…

HDU6092——Rikka with Subset 【dp】

题目网址&#xff1a; https://vjudge.net/problem/HDU-6092 As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta has nn positive A1−AnA1−An and their sum is mm…

洛谷P1725琪露诺【单调队列+dp】

题目描述 在幻想乡&#xff0c;琪露诺是以笨蛋闻名的冰之妖精。 某一天&#xff0c;琪露诺又在玩速冻青蛙&#xff0c;就是用冰把青蛙瞬间冻起来。但是这只青蛙比以往的要聪明许多&#xff0c;在琪露诺来之前就已经跑到了河的对岸。于是琪露诺决定到河岸去追青蛙。 小河可以看…

扶桑号战列舰【RMQ+分治】

扶桑号战列舰 时间限制: 1 Sec 内存限制: 128 MB Special Judge 提交: 197 解决: 63 [提交] [状态] [命题人:admin] 题目描述 众所周知&#xff0c;一战过后&#xff0c;在世界列强建造超无畏级战列舰的竞争之中&#xff0c;旧日本海军根据“个舰优越主义”&#xff0c;建造了扶…

大凤号装甲空母【找规律+矩阵快速幂】

大凤号装甲空母 时间限制: 1 Sec 内存限制: 128 MB 提交: 108 解决: 15 [提交] [状态] [命题人:admin] 题目描述 大凤号航空母舰很喜欢算术。 它&#xff0c;是旧日本海军中最为先进的航空母舰。 它&#xff0c;是旧日本海军中最为短命的航空母舰。 同时&#xff0c;她还是最平…

codevs5429 多重背包【多重背包+单调队列】

5429 多重背包 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 你有一个容量为M的背包&#xff0c;和N种物品。 每种物品都有三个属性&#xff0c;vi&#xff0c;wi&#xff0c;与ci&#xff0c;分别表示这种物品的体积、价值和件数。 你的…

generator 1【矩阵快速幂】

题目描述 You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0​,x1​,a,b. And you know xia⋅xi−1b⋅xi−2x_i a \cdot x_{i-1} b \cdot x_{i-2}xi​a⋅xi−1​b⋅xi−2​ for all i≥2i \ge 2i≥2. Given two positive integers n, and MOD, please calcul…

Give Candies【快速幂+欧拉】

Give Candies 时间限制: 1 Sec 内存限制: 128 MB 提交: 243 解决: 92 [提交] [状态] [命题人:admin] 题目描述 There are N children in kindergarten. Miss Li bought them N candies。To make the process more interesting, Miss Li comes up with the rule: All the childr…

Save the Room【找规律】

Save the Room 时间限制: 1 Sec 内存限制: 128 MB 提交: 149 解决: 90 [提交] [状态] [命题人:admin] 题目描述 Bob is a sorcerer. He lives in a cuboid room which has a length of A, a width of B and a height of C, so we represent it as ABC. One day, he finds that …

Participate in E-sports【Java大数+二分】

Participate in E-sports 时间限制: 2 Sec 内存限制: 128 MB 提交: 194 解决: 53 [提交] [状态] [命题人:admin] 题目描述 Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don’t know which one to choose, so they use a way to…

Poor God Water【矩阵快速幂】

Poor God Water 时间限制: 1 Sec 内存限制: 128 MB 提交: 102 解决: 50 [提交] [状态] [命题人:admin] 题目描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonou…

Transport Ship【多重背包】

Transport Ship 时间限制: 1 Sec 内存限制: 128 MB 提交: 175 解决: 65 [提交] [状态] [命题人:admin] 题目描述 There are N different kinds of transport ships on the port. The i^th kind of ship can carry the weight of V[i] and the number of the ith kind of ship i…

洛谷P2015 二叉苹果树【树形dp】

P2015 二叉苹果树 时间限制 1.00s 内存限制 125.00MB 题目描述 有一棵苹果树&#xff0c;如果树枝有分叉&#xff0c;一定是分2叉&#xff08;就是说没有只有1个儿子的结点&#xff09; 这棵树共有N个结点&#xff08;叶子点或者树枝分叉点&#xff09;&#xff0c;编号为1-N,…