XOR运算

近来做了一些题目和异或运算有关的题目,总结一下

Xor

按位异或
符号在编程语言中通常是 ^xor
数学符号通常用⊕表示
X = 0101B
Y = 1011B

XYX⊕B
101
110
000
011

X^Y = 1110B

性质

1. 0 ^ 0 = 0

2.a ^ a = 0

3.0 ^ 1 ^ 2 … ^ n的性质

先观察一下如下的 序列
我们暴力计算出前50项的异或和,观察规律

1 3 0 4 1 7 0 8 1 11 0 12 1 15 0 16 1 19 0 20 1 23 0 24 1 27 0 28 1 31 0 32 1 35 0 36 1 

不难得出每隔四项的异或和都为0
并且每四项都是以1 N-1 0 N这四项为规律
这里N取第一个大于等于n且是4的倍数的数
所以我们想要求得第1项到第n项的异或和的结果
只需要异或

n - n % 4 ^ (n - n % 4 + 1) ^... ^ n

while (n % 4 != 3 && n >= 0) {
       ans ^= n; n --;
}

4.[l,r]区间的异或和

l ^ l + 1 ^ l + 2^… ^ r = (1 ^ 2… ^ l ) ^ (1 ^ 2 ^ 3… ^ r)

所以如果我们要求一个区间段的异或和,只需要求(1-l) 的异或和 ^ (1-r)的异或和

证明
1 ^ 2 ^ 3 ... ^ l           
1 ^ 2 ^ 3... ^l ^ l+1 ^l+2 ....^ r-1 ^ r
很容易发现1^2^3...l相同,则异或和为0

传送门
XOR Sum

#include <iostream>

using namespace std;
typedef long long ll;
int main()
{
    ll a,b;
    cin >> a >> b;
    int kk = a % 4;
    a -= kk;
    ll ans = 0;
    for(ll i = a + kk;i < a + 4;i++)
    {
        ans ^= i;
    }
    int ss = b % 4;
    b -= ss;
    for(ll i = b;i <= b + ss;i++)
    {
        ans ^= i;
    }
    cout << ans;
    return 0;
}

Xor Sums

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;

void solved()
{
        ll ans = 0,n;
        cin >> n;
        while (n % 4 != 3 && n >= 0) {
            ans ^= n; 
            n --;
        }
}
 
int main() {
    	cin >> t;
        while (t --) 
        solved();
        return 0;
}

Blocks

#include <bits/stdc++.h>
      
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
  
using namespace std;
 
 
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
 
    string t;
    int n;
    cin >> n >> t;
 
    for (char c : {'W', 'B'}) {
        string s = t;
        vector<int> v;
        for (int i = 0; i < n - 1; i++) {
            if (s[i] != c) {
                v.push_back(i);
                s[i] = (s[i] == 'W' ? 'B' : 'W');
                s[i + 1] = (s[i + 1] == 'W' ? 'B' : 'W');
            }
        }
        if (s[n - 1] == c) {
            cout << v.size() << "\n";
            for (int x : v)
                cout << x + 1 << " ";
            cout << "\n";
            return 0;
        }
    }
    cout << -1;
    return 0;
}

Dr. Evil Underscores

#include <bits/stdc++.h>
#define N 100010
#define CINSPEED std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0)
using namespace std;
typedef long long ll;

int n;

int dfs(vector<int> t,int i)
{
    vector<int>one,zero;
    if(i < 0 || t.size() == 0)return 0;
    for(auto & j : t)
    {
        if(j >> i & 1)
        one.push_back(j);
        else zero.push_back(j);
    }
    if(!zero.size())return dfs(one,i - 1);
    if(!one.size())return dfs(zero,i - 1);
    return min(dfs(one,i - 1),dfs(zero,i - 1))|(1 << i);
}

int main()
{
    cin >> n;
    vector<int> k(n);
    for(int i = 0;i < n;i++)cin >> k[i];

    cout << dfs(k,30);
#ifdef LOCAL
    system("pause");
#endif
    return 0;    
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值