跳到正文

题解

题解:P16438 [XJTUPC 2026] 共同特征

Sunrise_up约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年5月10日
更新
2026年5月10日
篇幅
约 2 分钟
题目
P16438

版本

  1. v2当前
  2. v1

求点赞 qwq!

思路

题面看起来可怕,实际上问的问题很简单:给定 xx,求最小的正整数 yy,满足 (x and y)=gcd⁡(x,y)(x\ \mathrm{and}\ y)=\gcd(x,y)。

解法一

我们要最小正整数 yy,最小的正整数满足条件的,一定是 xx 二进制下最低位的 11,即 ymin⁡=lowbit(x)y_{\min}=\mathrm{lowbit}(x)。

解法二

观察样例,发现答案都是 2k2^k。

我们直接枚举 y=2ky=2^k 即可,范围是 0≤k≤600\le k\le 60,因为 1≤x<2601\le x<2^{60}。

代码

开 long long。

解法一

更推荐。

时间复杂度 O(T)O(T)。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,x;
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>T;
	while(T--)cin>>x,cout<<(x&-x)<<'\n';
}
解法二

时间复杂度 O(T)O(T),常数大。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int T,x;
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);
	cin>>T;
	while(T--){
		cin>>x;
		for(int i=0;i<61;i++){
			int y=powl(2,i);
			if((x&y)==__gcd(x,y)){
				cout<<y<<'\n';
				break;
			}
		}
	}
}
原文