题目分析
数据范围很小,可以直接从 (因为 ) 枚举到 ,每次令 ,然后检验 是否和 相等即可。
如何求
有一个函数叫 __builtin_popcountll(x),可以求出一个 long long 类型的变量 二进制下 的个数。
如何求
事实上,x&-x 即为 。
AC Code
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int input();
void print(int x);
signed main(void){
int n=input();//读入
for (int i = (1ll<<n)-1;i>=1;i--){//(1ll<<n) 即为 2 的 n 次方,因为数据范围,要用 1ll
int k=__builtin_popcountll(i);//统计 1 个数
if (k<=0) continue;//k 要为正整数
if ((i&-i)==(1ll<<k)){//如果相等
print(i);//输出
return 0;//结束
}
}
}
inline int input(){int x=0,f=1;char c=getchar();while (!isdigit(c)){if (c=='-') f=-1;c=getchar();}while (isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
void print(int x){if (x<0){putchar('-');x=-x;}if (x<10) putchar(x+'0');else{print(x/10);putchar(x%10+'0');}}