解题思路是:

Q=q1^q2.......^qn = p1^p2......^pn^((1%1)^....(1%n))^((2%1)^......(2%n))^....

故Q的求解过程分成两部分

第一部分是求p1^p2......^pn

第二部分是求((1%1)^....(1%n))^((2%1)^......(2%n))^....

将其化成矩形的形式

1%1   1%2  ...........  1%n

2%1   2%2  ............ 2%n

.....................................

n%1   n%2 ............. n%n

当i小于除数时,即 i%j = i (i<j)

故该矩形的上对角线变成

1%1     1  ...........      1              (n-1)个1

2%1   2%2  ............  2              (n-2)个2

.....................................             ........

(n-1)%1................   n-1

n%1   n%2 ............. n%n              0个n

注意偶数个相同的元素异或结果为0,奇数个相同元素异或结果为本身,0与其他元素异或结果为该元素

故对于矩形上三角 只需要考虑(n-i)的奇偶性 故这部分代码简化为

if(n-i是偶数) res^=i;

现在考虑矩形下三角,将下三角取余数的

0

0     0

0     1     0

.      0     1   ..

.      1     2

.      .      0     ...................

.      .      .

0       .........................................

根据规律

第一列为0循环变化

第二列为0~1循环变化

第i列为 0 ~i-1 循环变化

...........

第i列元素个数为n-i+1

得到每列的循环个数及剩余的个数

判断循环个数的奇偶,注意偶数个相同的元素异或结果为0,奇数个相同元素异或结果为1,0与任何元素异或结果为该元素

然后将剩余的个数异或即可

#include <iostream>
#include <vector>
using namespace std; int main(){
int n,res = ,p;
cin>>n;
vector<int> table(n+,);
for(int i = ; i <= n; ++ i){
cin >> p; res^=p; //针对p1^p2......^pn
if((n-i)%) res^=i; //针对上三角
table[i]^=table[i-]^i; //打表
}
int a = ;
for(int i = ; i < n ; ++ i){ //针对下三角
int num = (n-i)/a, leave = (n-i)%a;
if(num%) res^=table[a-];
if(leave) res^=table[leave-];
a++;
}
cout<<res<<endl;
}

Codeforces Round #242 (Div. 2) C. Magic Formulas的更多相关文章

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

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

  2. Codeforces Round #242 (Div. 2) A~C

    题目链接 A. Squats time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputou ...

  3. Codeforces Round #242 (Div. 2) &lt;A-D&gt;

    CF424 A. Squats 题目意思: 有n(n为偶数)个x和X,求最少的变换次数,使得X的个数为n/2,输出变换后的序列. 解题思路: 统计X的个数ans,和n/2比較,少了的话,须要把n/2- ...

  4. Codeforces Round #335 (Div. 2) A. Magic Spheres 水题

    A. Magic Spheres Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606/ ...

  5. Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算

    D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...

  6. Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分

    D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...

  7. Codeforces Round #350 (Div. 2) D2. Magic Powder - 2

    题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...

  8. Codeforces Round #242 (Div. 2) C题

    题目链接:http://codeforces.com/contest/424/problem/C, 想来一个小时,就是做不出,都做出来了,悲剧! 分析:我们知道交换异或的顺序不影响答案! 然后就是求t ...

  9. Codeforces Round #335 (Div. 2) A. Magic Spheres 模拟

    A. Magic Spheres   Carl is a beginner magician. He has a blue, b violet and c orange magic spheres. ...

随机推荐

  1. nginx 配一个简单的静态文件服务器 和一个虚似机

    下面是个图片服务器: server { listen ; server_name img.xxx.xxx.com; root /data/site/img.xxx.xxx.com; access_lo ...

  2. C#回顾 - 2.NET的IO:Path、File、FileInfo、Directory、DirectoryInfo、DriveInfo、FileSystemWatcher

        1.管理文件系统 一般而言,应用程序都会有保存数据.检索数据的需求. 1.1 使用 path 类来访问文件路径 [path常用的方法]:http://www.cnblogs.com/tangg ...

  3. C#的LINQ to Object

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  4. 用with实现python的threading,新鲜啊

    哈哈,2.5以后可用.自动加锁释放,如同操作文件打开关闭一样. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading impor ...

  5. hibernate查询语句实例代码

    一.聚集函数的使用: avg(...), sum(...), min(...), max(...) count(*) count(...), count(distinct ...), count(al ...

  6. 堆栈C实现

    标准C语言没有像C++那样可以直接调用的STL容器,所以在c语言中实现容器功能就得自己去定义堆栈结构: stack.h /************this head file defines a st ...

  7. Linux环境下stl库使用(map)

    例子1: testMap.cpp #include <string.h> #include <iostream> #include <map> #include & ...

  8. android 入门-android Studio git 克隆

    最后是完成 以上是如何从android studio Git 克隆Github的项目

  9. 用PowerShell脚本删除SharePoint 的 Page中的WebPart

    编写PowerShell脚本可以删除page中所有的webpart,也可以根据webpart的属性信息去删除特定的webpart. 下面的PowerShell脚本便是删除对应page中所有的webpa ...

  10. Codeforces Round #370 (Div. 2) D. Memory and Scores DP

    D. Memory and Scores   Memory and his friend Lexa are competing to get higher score in one popular c ...