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. QQ空间技术架构之深刻揭秘

    QQ空间技术架构之深刻揭秘 来源: 腾讯大讲堂  发布时间: 2012-05-17 17:24  阅读: 7822 次  推荐: 4   [收藏]   QQ 空间作为腾讯海量互联网服务产品,经过近七年 ...

  2. C语言中的指针与整数相加的值计算

    以下分三种情况: 1. 指针 + 整数值 2. 整数 + 整数  3. 指针强制转换为另一个类型后(指针或者是整数)  +  整数 测试例子: 1 struct AAA{ int a; char b[ ...

  3. TensorFlow 2.0 深度学习实战 —— 浅谈卷积神经网络 CNN

    前言 上一章为大家介绍过深度学习的基础和多层感知机 MLP 的应用,本章开始将深入讲解卷积神经网络的实用场景.卷积神经网络 CNN(Convolutional Neural Networks,Conv ...

  4. Factorization

    Factorization or factoring consists of writing a number or another mathematical object as a product ...

  5. Flink(五) 【消费kafka】

    目录 0.目的 1.本地测试 2.线上测试 提交作业 0.目的 测试flink消费kafka的几种消费策略 kafkaSource.setStartFromEarliest() //从起始位置 kaf ...

  6. vi查找替换命令详解 (转载)

    转载至:   http://blog.csdn.net/lanxinju/article/details/5731843 一.查找 查找命令 /pattern<Enter> :向下查找pa ...

  7. 关于stm32不常用的中断,如何添加, 比如timer10 timer11等

    首先可以从keil中找到 比如找到定时器11的溢出中断,如上图是26 然后,配置定时器11 溢出中断的时候,我就在:下面填上这个变量. 之后要写中断服务函数,也就是发生中断后要跳转到的函数. 需要知道 ...

  8. 快速挂起VIM以及调出被挂起的VIM的方法

    vim中开了多窗口后有时需要临时切出去执行shell指令,查看结果,在vim中用%很不方便查看结果,要切出去又要逐个小窗口:q,非常麻烦. 上网一查竟然有挂起的方法: 挂起:ctrl-z 调出:fg ...

  9. springboot-MVC 过滤器使用

    一.前言 一下代码以SSO用户登录列子代码.完整代码https://gitee.com/xuxueli0323/xxl-sso 二.使用 2.1 创建过滤器 创建一个过滤器,实现Filter 接口 p ...

  10. idea开发环境搭建ssh

    idea2020完整web开发(struts2+spring+hibernate) idea破解 第一步: 下载最新的 IDEA 2020.3.2 版本安装包 https://www.jetbrain ...