洛谷P3908 异或之和

版权声明:转载无所谓的。。。 https://blog.csdn.net/xuxiayang/article/details/83420404

大意

1   x o r   2   x o r   3   x o r   4   x o r   5 x o r   n 1\ xor\ 2\ xor\ 3\ xor\ 4\ xor\ 5……xor\ n 的值


思路

这道题要是放在比赛绝对大把大把人 A A

为什么呢?

因为手推都可以推出规律啊。。。

咳咳,开始讲证明

假设我们只看每个数的后两位,必定是
00 , 01 , 10 , 11 00,01,10,11
所以连续的四个数异或起来必定是0,所以我们就只需考虑 n m o d    4 n\mod 4 进行分类讨论即可


代码

#include<cstdio>
using namespace std;long long n;
signed main()
{
	scanf("%lld",&n);
	if(n%4==0) printf("%lld",n);//剩下个n
	if(n%4==1) putchar(49);//剩下个n和n-1,后两位分别是0,0和0,1,异或起来即为1
	if(n%4==2) printf("%lld",n+1);//剩下n-2,n-1,n,后两位分别是0,0和0,1及1,0,前面两个异或变成1,1异或一个偶数等于这个偶数+1
	if(n%4==3) putchar(48);//全部刚好抵掉
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/83420404