参考博客:类欧几里得算法小结
例题:HDU 6275 Mod, Xor and Everything
题目描述
You are given an integer n.
You are required to calculate (n mod 1) xor (n mod 2) xor ... xor (n mod (n - 1)) xor (n mod n).
The “xor” operation means “exclusive OR”.
输入
The first line contains an integer T (1≤T≤5) representing the number of test cases.
For each test case, there is an integer n (1≤n≤1012) in one line.
输出
For each test case, print the answer in one line.
推导过程主要参考以上博客:
但是有些细节我觉得自己还是不太明白,自己重新写一次博客加深印象!!!!
1、F(a,b,c,n)
1、当 a>=c 或者 b>=c 时:
(此时,a和b都可以分成 整除部分 和 余数部分 表示 ):
2、当 a<c 并且 b<c 时:
在推导之前先给大家一个思想用不等式求和的方式更换一个数:
譬如:
左边: 等差数列求和 , 右边: i 分解成从1~i 个1累加
i=1时: j = 1 满足条件. i = 1 = 1
i=2时: j = 1 , 2 满足条件 i = 1 + 1 = 2
i=3时: j = 1 , 2 , 3 满足条件 i = 1 + 1 + 1 = 3
i=4时: j = 1 , 2 , 3 , 4满足条件 i = 1 + 1 + 1 + 1 = 4
开始推导:
换元法:令 k = j-1 , 因为变量名字可以换回来,所以 j = j - 1
符号两边同时乘以C 不改变 符号方向:同时满足以下式子:
把在乘法过程把下取整符号去掉
把≥变成>