---异或和---

今天又又又学到新东西了

异或和

D-嘤嘤不想求异或喵_牛客周赛 Round 49 (nowcoder.com)

这是个挺经典的题目。

x^x=0.0^x=x;

#include<iostream> 
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
using ll = long long;
using namespace std;
const int N = 1e6;
ll a[N];
ll b[N];
ll f(ll x)
{
    if(x%4==1)return 1;
    else if(x%4==2)return x+1;
    else if(x%4==3)return 0;
    else return x;
}
void solve()
{
    ll l,r;
    cin>>l>>r;
    ll ans=f(l-1)^f(r);
    cout<<ans<<"\n";
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

完整代码

通过打表找出规律

然后类似与前缀和的方式

令f(l-1)=x->(前l-1个数异或),f(r)=y->(前r个数异或);

要求l到r的异或

f(l-1)^f(r-l)=f(r);

f(l-1)^f(r-l)^f(l-1)=f(r)^f(l-1);

f(r-l)=f(r)^f(l-1);

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值