题目描述:给定n,求最大的k使得
n & (n−1) & (n−2) & (n−3) & ... (k) = 0
大胆猜测当且仅当n的最高位被0与掉了的时候这个式子会等于0,所以k满足2^m-1<n<=2^(m+1)-1,找到这个k=2^m-1输出即可。
#include<bits/stdc++.h> #define ll long long using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); ll t,n; ll k; cin>>t; while(t--) { cin>>n; k=0; while(k<n) {k=(k<<1)+1;} k>>=1; cout<<k<<endl; } //getchar();getchar(); return 0; }