codeforce-424C. Magic Formulas(数学)
standard input
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.
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).
The only line of output should contain a single integer — the value of Q.
3
1 2 3
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(数学)的更多相关文章
- CodeForce 424C Magic Formulas
这个题就是求出给的公式的结果. 仅仅要知道异或运算满足交换律跟结合律即可了.之后就是化简公式. #include<map> #include<string> #include& ...
- Codeforce 424C Magic Formulas 找规律
题目链接:http://codeforces.com/contest/424/problem/C 题意:求Q值 思路:找规律 显然能够得到一个矩阵 把这个矩阵画出来就能发现一个横向的规律和一个主对角线 ...
- Codeforces 424 C. Magic Formulas
xor是满足交换律的,展开后发现仅仅要能高速求出 [1mod1....1modn],....,[nmod1...nmodn]的矩阵的xor即可了....然后找个规律 C. Magic Formulas ...
- Codeforces Round #242 (Div. 2) C. Magic Formulas
解题思路是: Q=q1^q2.......^qn = p1^p2......^pn^((1%1)^....(1%n))^((2%1)^......(2%n))^.... 故Q的求解过程分成两部分 第一 ...
- Codeforces Round #242 (Div. 2) C. Magic Formulas (位异或性质 找规律)
题目 比赛的时候找出规律了,但是找的有点慢了,写代码的时候出了问题,也没交对,还掉分了.... 还是先总结一下位移或的性质吧: 1. 交换律 a ^ b = b ^ a 2. 结合律 (a^b) ^ ...
- codeforce 606A - Magic Spheres
题意:a,b,c三种球,能把俩个一样的球变成另一颜色不一样的球.给你目标x,y,z,问能否经过变化至少达打目标. #include<iostream> #include<stdio. ...
- cf C. Magic Formulas
http://codeforces.com/contest/424/problem/C #include <cstdio> #include <cstring> #includ ...
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学
题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...
- codeforces C. Magic Formulas 解题报告
题目链接:http://codeforces.com/problemset/problem/424/C 题目意思:给出 n 个数:p1, p2, ..., pn,定义: q1 = p1 ^ (1 mo ...
随机推荐
- 分布式事务(3)---强一致性分布式事务Atomikos实战
分布式事务(1)-理论基础 分布式事务(2)---强一致性分布式事务解决方案 分布式事务(4)---最终一致性方案之TCC 前面介绍强一致性分布式解决方案,这里用Atomikos框架写一个实战的dem ...
- 10 — springboot整合mybatis — 更新完毕
1.xml版 -- 复杂sql使用xml,简单sql使用注解 1).导入依赖 <!-- mybatis-spring-boot-starter是第三方( mybatis )jar包,不是spri ...
- SELECT的语法
我们先回顾下正则表达式.下图: 描述像xy, xxy (B上转一圈), xyy, xxyy这样的字符串.然后可以进行字符串匹配.设计芯片都用Verilog语言而不是画门电路了.像x+y+这样的叫做re ...
- @Data 注解引出的 lombok
今天在看代码的时候, 看到了这个注解, 之前都没有见过, 所以就查了下, 发现还是个不错的注解, 可以让代码更加简洁. 这个注解来自于 lombok,lombok 能够减少大量的模板代码,减少了在使用 ...
- sql技巧(增册改查)
1 select * from wyl.t; 2 --将数据从t1导入t2 3 insert into t2(c1,c2) select c1,c2 from t1 where c1= xx and ...
- Spring Boot中使用模板引擎Thymeleaf
一.Thymeleaf简介 Thymeleaf[taɪm lif],百里香叶,是一个流行的模板引擎,该模板引擎采用Java语言开发.Java中常见的模板引擎有Velocity.Freemaker.Th ...
- 【Linux】【Problems】在fedora 9上解决依赖问题
summary: 在32bit的fedora9上安装EMC客户端遇到无法解决的依赖问题 detail: rpm 安装: [root@hcszmons02 tmp]# rpm -ivh lgtoclnt ...
- feign中开启熔断的书写步骤
/** 1.在pom.xml中引入依赖 2.在application.yaml中开启hystrix 3.在方法上配置熔断类 4.书写接口的实现类 **/ //1.在pom.xml中引 ...
- python数据预处理和特性选择后列的映射
我们在用python进行机器学习建模时,首先需要对数据进行预处理然后进行特征工程,在这些过程中,数据的格式可能会发生变化,前几天我遇到过的问题就是: 对数据进行标准化.归一化.方差过滤的时候数据都从D ...
- kubeadm安装k8s集群
安装kubeadm kubectl kubelet 对于Ubuntu/debian系统,添加阿里云k8s仓库key,非root用户需要加sudo apt-get update && a ...