【题解】【洛谷B4261】 [GESP202503 三级] 2025
[GESP202503 三级] 2025
题目描述
小 A 有一个整数 x x x,他想找到最小的正整数 y y y 使得下式成立:
( x and y ) + ( x or y ) = 2025 (x \ \operatorname{and} \ y) + (x \ \operatorname{or} \ y) = 2025 (x and y)+(x or y)=2025
其中 and \operatorname{and} and 表示二进制按位与运算, or \operatorname{or} or 表示二进制按位或运算。如果不存在满足条件的 y y y,则输出 − 1 -1 −1。
输入格式
一行,一个整数 x x x。
输出格式
一行,一个整数,若满足条件的 y y y 存在则输出 y y y,否则输出 − 1 -1 −1。
输入输出样例
输入 #1
1025
输出 #1
1000
说明/提示
对于所有测试点,保证 0 ≤ x < 2025 0 \leq x < 2025 0≤x<2025。
( x and y ) + ( x or y ) = 2025 (x \ \operatorname{and} \ y) + (x \ \operatorname{or} \ y) = 2025 (x and y)+(x or y)=2025
其中:
- and \operatorname{and} and 表示按位与运算,运算符为 & \& &。
- or \operatorname{or} or 表示按位或运算,运算符为 ∣ | ∣。
1.思路解析
一道非常简单的模拟
+
+
+枚举的题目。直接从y=1开始枚举就行了。
由于
(
x
and
y
)
+
(
x
or
y
)
=
2025
(x \ \operatorname{and} \ y) + (x \ \operatorname{or} \ y) = 2025
(x and y)+(x or y)=2025这个式子是单调递增的。(即结果随着x的增大而增大)。
那么当结果=2025时,达到结果,输出。当结果>2025时,跳出循环。代码十分容易实现。
2.AC代码
#include<bits/stdc++.h>
using namespace std;
int x,num,ans=-1; // 答案默认设置为-1
int main(){
cin>>x;
for(int y=1;num<2025;y++){ // 枚举可能的y值
num=(x&y)+(x|y);
if(num==2025){ans=y;break;} // 得到答案,跳出
}
cout<<ans;
return 0;
}
最后,制作不易,希望大家多多点赞收藏,关注下微信公众号,谢谢大家的关注,您的支持就是我更新的最大动力!
公众号上会及时提供信息学奥赛的相关资讯、各地科技特长生升学动态、还会提供相关比赛的备赛资料、信息学学习攻略等。
2159

被折叠的 条评论
为什么被折叠?



