P6861 [RC-03] 难题

R e s u l t Result Result

...


D e s c r i p t i o n Description Description

给定一个数 n n n,要求在 [ 1 , n ] [1,n] [1,n]范围内选两个正整数,使得二者或和+异或和最大

数据范围: n ≤ 1 0 18 n\leq 10^{18} n1018


S o l u t i o n Solution Solution

显然这两者的最大值必然都是 2 k − 1 2^{k}-1 2k1 k k k n n n在二进制下的位数

时间复杂度: O ( l o g n ) O(logn) O(logn)


C o d e Code Code
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;LL n,now=1;
inline LL read()
{
	char c;LL d=1,f=0;
	while(c=getchar(),!isdigit(c)) if(c=='-') d=-1;f=(f<<3)+(f<<1)+c-48;
	while(c=getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
	return d*f;
}
signed main()
{
	n=read();
	while((now<<1)-1<n) now<<=1;
	now=(now<<1)-1;
	printf("%lld",now<<1);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值