HDU 5683 zxa and xor (模拟)

本文介绍了一种使用模拟算法解决序列XOR运算的问题,通过更新元素值并重新计算所有可能的两两元素之和的XOR结果来求解每次变更后的序列状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:这题居然能模拟...



#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e4+6;
int a[maxn],ans,n,m;
void solve()
{
    scanf("%d%d",&n,&m);
    ans=0;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)for(int j=i+1;j<=n;j++)
        ans^=(a[i]+a[j]);
    while(m--)
    {
        int x,y;scanf("%d%d",&x,&y);
        for(int i=1;i<x;i++)ans^=(a[x]+a[i])^(y+a[i]);
        for(int i=x+1;i<=n;i++)ans^=(a[x]+a[i])^(y+a[i]);
        a[x]=y;
        printf("%d\n",ans);
    }
}
int main()
{
    int t;scanf("%d",&t);
    while(t--)solve();
    return 0;
}

Problem Description
zxa had a great interest in exclusive disjunction(i.e. XOR) recently, therefore he took out a non-negative integer sequence  a1,a2,,an  of length  n .

zxa thought only doing this was too boring, hence a function  funct(x,y)  defined by him, in which  ax  would be changed into  y  irrevocably and then compute  1i<jn(ai+aj)  as return value.

zxa is interested to know, assuming that he called such function  m  times for this sequence, then what is the return value for each calling, can you help him?

tips: 1i<jn(ai+aj)  means that  (a1+a2)(a1+a3)(a1+an)(a2+a3)(a2+a4)(a2+an)(an1+an) .
 

Input
The first line contains an positive integer  T , represents there are  T  test cases.

For each test case:

The first line contains two positive integers  n  and  m .

The second line contains  n  non-negative integers, represent  a1,a2,,an .

The next  m  lines, the  i -th line contains two non-negative integers  x  and  y , represent the  i -th called function is  funct(x,y) .

There is a blank between each integer with no other extra space in one line.

1T1000,2n2104,1m2104,0ai,y109,1xn,1n,m105
 

Output
For each test case, output in  m  lines, the  i -th line a positive integer, repersents the return value for the  i -th called function.
 

Sample Input
  
1 3 3 1 2 3 1 4 2 5 3 6
 

Sample Output
  
4 6 8
Hint
After the first called function, this sequence is $\{4,2,3\}$, and $(4+2)\otimes(4+3)\otimes(2+3)=4$. After the second called function, this sequence is $\{4,5,3\}$ and $(4+5)\otimes(4+3)\otimes(5+3)=6$. After the third called function, this sequence is $\{4,5,6\}$ and $(4+5)\otimes(4+6)\otimes(5+6)=8$.
 

Source
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值