Codeforces Round #242 (Div. 2)C(找规律,异或运算)

一看就是找规律的题。只要熟悉异或的性质,可以秒杀。

为了防止忘记异或的规则,可以把异或理解为半加运算:其运算法则相当于不带进位的二进制加法。

一些性质如下:

交换律A oplus B = B oplus A

结合律A oplus (B oplus C)=(A oplus B) oplus C

恒等律Xoplus 0=X

归零律Xoplus X=0

典型应用:交换a和b的值:a=a^b^(b=a);

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
int n,p;
int ans=0;
int x[1000009];
int main()
{
    //freopen("in7.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    scanf("%d",&n);
    x[0]=0;
    for(int i=1; i<=n; i++)
    {
        x[i]=x[i-1]^(i-1);
        int t=n/i;
        if(t%2==1)      ans^=x[i];
        ans^=x[n%i+1];
    }
    for(int i=0; i<n; i++)
    {
        scanf("%d",&p);
        ans^=p;
    }
    printf("%d
",ans);
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
原文地址:https://www.cnblogs.com/zywscq/p/3963460.html