HDU 5344 MZL's xorti
2015-08-04 20:02
302 查看
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)(1≤i,j≤n)
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=(Ai−1∗m+z) mod l
1≤m,z,l≤5∗105,n=5∗105
Output
For every test.print the answer.
Sample Input
2
3 5 5 7
6 8 8 9
Sample Output
14
16
Source
2015 Multi-University Training Contest 5
/*********************************************************************/
题意:
给你四个整数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;
}欢迎大家点评
菜鸟成长记
相关文章推荐
- hdu5351(2015多校5)--MZL's Border(打表,,找规律)
- windows获取其他程序窗口的内容
- opencv学习(三)书本《学习Opencv》(中文版)第四章的样例Ex4-1(opencv3.0.0+VS2012+win7)
- Objective-C学习-字典类型 NSDictionary 和 NSMutableDictionary
- 如何设计魔方
- 雷铭CMS内容管理系统简介
- [转]Java程序员从笨鸟到菜鸟之(八十三)细谈Spring(十二)OpenSessionInView详解及用法
- 2015 多校联赛 ——HDU5344(水)
- 针对报出 cvc-complex-type.2.4.a: Invalid content was found starting with element 问题解决办法
- 2015 多校联赛 ——HDU5344(水)
- poj-2253-Frogger-最短路
- 反射、注解和线程、Thread、同步锁问题和基于多线程的Server通信(8.4)
- [算法专题] 二分搜索&排序数组
- UISlider 和 UISegement
- bzoj [NOI2007]货币兑换Cash (cdq分治+斜率优化 )
- SDKD Summer Team Contest B
- [Jobdu] 题目1367:二叉搜索树的后序遍历序列
- HDU5137
- 第一天 ted英语学习Chris Milk: How virtual reality can create the ultimate empathy machine
- 《Windows Internals》第六版 Chapter 6 Security 笔记