P5514 [MtOI2019]永夜的报应

看了题解才知道这个性质:
a ⊕ b ≤ a + b a \oplus b \le a+b aba+b
粗略证明:

0 + 0 = 0 , 0 ⊕ 0 = 0 1 + 0 = 1 , 1 ⊕ 0 = 1 0 + 1 = 1 , 0 ⊕ 1 = 1 1 + 1 = 2 , 1 ⊕ 1 = 0 0+0=0,0\oplus 0=0\\ 1+0=1,1\oplus 0=1\\ 0+1=1,0\oplus 1=1\\ 1+1=2,1\oplus 1=0 0+0=0,00=01+0=1,10=10+1=1,01=11+1=2,11=0
可以得到: a ⊕ b ≤ a + b a \oplus b \le a+b aba+b(进行运算的数二进制下每一位上都满足上述的规则)

回到题目,设分成了2组,得到结果为c,d,原数为 a 1 , a 2 , a 3 , a 4 a_1,a_2,a_3,a_4 a1,a2,a3,a4,则 c + d = ( a 1 ⊕ a 2 ) + ( a 3 ⊕ a 4 ) ≥ a 1 ⊕ a 2 ⊕ a 3 ⊕ a 4 c + d =(a_1 \oplus a_2)+(a_3 \oplus a_4)\ge a_1 \oplus a_2 \oplus a_3 \oplus a_4 c+d=(a1a2)+(a3a4)a1a2a3a4

代码:

#include <bits/stdc++.h>

using namespace std;
int n,ans,p;

int main()
{
	cin>>n;
	cin>>ans;
	for(int i=2;i<=n;i++)
	{
		cin>>p;
		ans=ans^p;
	}
	cout<<ans;
	
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值