C. Magic Formulas
time limit per test:2 seconds     memory limit per test:256 megabytes
 
input

standard input

output

standard output

People in the Tomskaya region like magic formulas very much. You can see some of them below.

Imagine you are given a sequence of positive integer numbers p1, p2, ..., pn. Lets write down some magic formulas:

Here, "mod" means the operation of taking the residue after dividing.

The expression  means applying the bitwise xor (excluding "OR") operation to integers x and y. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by "^", in Pascal — by "xor".

People in the Tomskaya region like magic formulas very much, but they don't like to calculate them! Therefore you are given the sequence p, calculate the value of Q.

Input

The first line of the input contains the only integer n (1 ≤ n ≤ 106). The next line contains n integers: p1, p2, ..., pn (0 ≤ pi ≤ 2·109).

Output

The only line of output should contain a single integer — the value of Q.

Sample test(s)
input
3
1 2 3
output
3

 为公式2.

因为异或运算满足交换律和结合律,

所以公式 = p1^p2^……^pn^(1%1)^(1%2)^……(1%n)^(2%1)%(2%2)^……^(2%n)^……^(n%1)^(n%2)^……^(n%n)

=p1^p2^……^pn^(1%1)^(2%1)^……(n%1)^(1%2)^(2%2)^……^(n%2)^……^(1%n)%(2%n)^……^(n%n).

公式2=(1%1)^(1%2)^……(1%n)^(2%1)%(2%2)^……^(2%n)^……^(n%1)^(n%2)^……^(n%n)

=p1^p2^……^pn^(1%1)^(2%1)^……(n%1)^(1%2)^(2%2)^……^(n%2)^……^(1%n)%(2%n)^……^(n%n).

那么公式2再以后面模数分类结合。

可以的到模数从1-n的通项为Sk=(1%k)^(2%k)^.....(n%k).

那么k是从1-n取不同的数,然后公式2就为S1^S2.....^Sn

关键是Sk的求解,每一个k,Sk=(1%k)^(2%k)^.....(n%k),其中(n%k)的值只能取(0-k),那没 (1,n)是连续的所以必定有周期,

由于a^a=0,a^0=a;

举个例子;假如n=9,k=4;

1 2 3 0
1 2 3 0
1      
       

结果就是1,上边的两行a^a=0,所以只剩下1;

又如当n=25,k=7时。(1%7)、(2%7)……(25%7)的结果如下表。

1

2

3

4

5

6

0

1

2

3

4

5

6

0

1

2

3

4

5

6

0

1

2

3

4

 

上边的整行就是n/k;

下面剩下的就是n%k;

那么如果上面为偶数行&&(n%k)>=1的话那么最后的sk=1^...(n%k);

如果上面为偶数行&&n%k==0,那么sk=0;

如果上面为奇数行&&(n%k)==0,sk=1^...k;

如果上面为奇数行&&(n%k)>=1,那么sk=(n%k+1)^....k;

这样的话每个sk就很容易计算了。

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<stdlib.h>
4 #include<string.h>
5 #include<iostream>
6 #include<queue>
7 #include<math.h>
8 typedef long long ll;
9 ll a[1000005];
10 ll dd[1000005];
11 ll cc[1000005];
12 int main(void)
13 {
14 ll i,j,k,p,q;
15 ll pp=0;
16 dd[0]=0;
17 while(scanf("%I64d",&k)!=EOF)
18 {
19 cc[k+1]=0;
20 for(i=1; i<=k; i++)
21 {
22 scanf("%I64d",&a[i]);
23 pp^=a[i];
24 dd[i]=(dd[i-1]^i);
25 }
26 for(i=k; i>=0; i--)
27 {
28 cc[i]=cc[i+1]^i;
29 }
30 /*那么如果上面为偶数行&&(n%k)>=1的话那么最后的sk=1^...(n%k);
31
32 如果上面为偶数行&&n%k==0,那么sk=0;
33 如果上面为奇数行&&(n%k)==0,sk=1^...k;
34
35 如果上面为奇数行&&(n%k)>=1,那么sk=(n%k+1)^....k;*/
36 for(i=1; i<=k; i++)
37 {
38 p=k/i;
39 q=k%i;
40 if(p%2==1)
41 {
42 pp^=(cc[i]^cc[q+1]);
43 }
44 else if(p%2==0)
45 {
46 pp^=dd[q];
47 }
48 }
49 printf("%I64d\n",pp);
50 }
51 return 0;
52 }

codeforce-424C. Magic Formulas(数学)的更多相关文章

  1. CodeForce 424C Magic Formulas

    这个题就是求出给的公式的结果. 仅仅要知道异或运算满足交换律跟结合律即可了.之后就是化简公式. #include<map> #include<string> #include& ...

  2. Codeforce 424C Magic Formulas 找规律

    题目链接:http://codeforces.com/contest/424/problem/C 题意:求Q值 思路:找规律 显然能够得到一个矩阵 把这个矩阵画出来就能发现一个横向的规律和一个主对角线 ...

  3. Codeforces 424 C. Magic Formulas

    xor是满足交换律的,展开后发现仅仅要能高速求出 [1mod1....1modn],....,[nmod1...nmodn]的矩阵的xor即可了....然后找个规律 C. Magic Formulas ...

  4. Codeforces Round #242 (Div. 2) C. Magic Formulas

    解题思路是: Q=q1^q2.......^qn = p1^p2......^pn^((1%1)^....(1%n))^((2%1)^......(2%n))^.... 故Q的求解过程分成两部分 第一 ...

  5. Codeforces Round #242 (Div. 2) C. Magic Formulas (位异或性质 找规律)

    题目 比赛的时候找出规律了,但是找的有点慢了,写代码的时候出了问题,也没交对,还掉分了.... 还是先总结一下位移或的性质吧: 1.  交换律 a ^ b = b ^ a 2. 结合律 (a^b) ^ ...

  6. codeforce 606A - Magic Spheres

    题意:a,b,c三种球,能把俩个一样的球变成另一颜色不一样的球.给你目标x,y,z,问能否经过变化至少达打目标. #include<iostream> #include<stdio. ...

  7. cf C. Magic Formulas

    http://codeforces.com/contest/424/problem/C #include <cstdio> #include <cstring> #includ ...

  8. Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学

    题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...

  9. codeforces C. Magic Formulas 解题报告

    题目链接:http://codeforces.com/problemset/problem/424/C 题目意思:给出 n 个数:p1, p2, ..., pn,定义: q1 = p1 ^ (1 mo ...

随机推荐

  1. A Child's History of England.45

    To forgive these unworthy princes was only to afford them breathing-time for new faithlessness. They ...

  2. NSString类里有个hash

    实际编程总会涉及到比较两个字符串的内容,一般会用 [string1 isEqualsToString:string2] 来比较两个字符串是否一致.对于字符串的isEqualsToString方法,需要 ...

  3. 关于java构造器

    关于java的构造器.首先构造器并不会创建java对象,构造器知识负责执行初始化,在构造器执行之前,Java对象所需要的内存空间是由new关键字申请出来的.大部分时候,程序使用new关键字为一个Jav ...

  4. malloc() vs new

    Following are the differences between malloc() and operator new. (1)new calls constructors, while ma ...

  5. 索引以及Mysql中的索引

    一.什么是索引 索引是表的目录,会保存在额外的文件中,针对表中的指定列建立,专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取 ...

  6. IIS 发布 WebService 连接DB2数据库报错如下图

    环境描述: 系统环境: Windows Server 2012 R2      IIS版本:IIS 6.2      C#环境:.NET Framework 4  DB2版本:9.7.500.702 ...

  7. 【Spark】【设置】关闭INFO提示

    目的:关闭INFO提示 方法:通过修改配置文件实现 操作文件:Hadoop/conf/log4j.properties.template 操作1:复制模板文件使用 cp $SPARK_HOME/con ...

  8. Redis单点到集群迁移

    目录 一.简介 一.简介 1.环境 源 192.168.1.185的6379 目标 192.168.1.91的7001,7002 192.168.1.92的7003,7004 192.168.1.94 ...

  9. pipeline groovy

    目录 一.变量 一.变量 1.直接定义 def x="abc" 2.从脚本执行结果赋值变量 branch = "/jen_script/return-branch.sh ...

  10. Nginx模块之Nginx-echo

    目录 一.简介 二.使用 一.简介 官网 Nginx-echo可以在Nginx中用来输出一些信息,是在测试排错过程中一个比较好的工具.它也可以做到把来自不同链接地址的信息进行一个汇总输出.总之能用起来 ...