HDU 5344 MZL's xorti

本文探讨了异或运算在序列生成问题中的应用,详细解释了如何通过给定的整数参数计算特定序列的异或和。文章通过实例演示了解题思路,并提供了相应的代码实现。

MZL's xor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to know the xor of all ( Ai + Aj )( 1i,jn )
The xor of an array B is defined as  B1  xor  B2 ...xor  Bn
 

Input
Multiple test cases, the first line contains an integer T(no more than 20), indicating the number of cases.
Each test case contains four integers: n , m , z , l
A1=0 , Ai=(Ai1m+z)   mod   l
1m,z,l5105 , n=5105
 

Output
For every test.print the answer.
 

Sample Input
  
2 3 5 5 7 6 8 8 9
 

Sample Output
  
14 16
 

Source
 
/*********************************************************************/

题意:

给你四个整数n,m,z,l,已知A1=0,利用公式Ai=(Ai-1*m+z)%l求出A2到An,题目要求计算所有(Ai+Aj)(1<=i,j<=n)异或的结果。

解题思路:

首先我们先假设n=3来分析一下这道题的解法,n=3的时候我们可以得到A1,A2,A3三个数

那我们要求的其实就是

(A1+A1)^(A1+A2)^(A1+A3)^(A2+A1)^(A2+A2)^(A2+A3)^(A3+A1)^(A3+A2)^(A3+A3)

我们移动一下位置或许题目会更加明朗

(A1+A1)^(A2+A2)^(A3+A3)^(A1+A2)^(A2+A1)^(A1+A3)^(A3+A1)^(A2+A3)^(A3+A2)

我们很容易发现红色部分两两异或之后的结果都为0,从中可以发现此题其实要求的就是求

(A1+A1)^(A2+A2)^……^(An+An)=(A1^A2^……^An)*2

还有一点要注意的是算Ai的过程中会超过int范围,相信大家都知道怎么处理这个问题,好了,闲话不多说,上代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 500005;
const int inf = 1000000000;
int main()
{
    __int64 n,m,z,l,t,sum,i,a;
    scanf("%I64d",&t);
    while(t--)
    {
        sum=a=0;
        scanf("%I64d%I64d%I64d%I64d",&n,&m,&z,&l);
        for(i=2;i<=n;i++)
        {
            a=(a*m+z)%l;
            sum^=a;
        }
        printf("%I64d\n",sum*2);
    }
    return 0;
}
欢迎大家点评

菜鸟成长记

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值