3 条题解

  • 0
    @ 2026-8-3 13:39:44

    位运算的性质,n位2的整数幂减一以后一定 得到一个全为1的n-1位二进制数,再与运算一下若为0则说明这是一个2的整数幂,不为0则说明不是,比如6的二进制为101,减去1为100,与运算得到4,8的二进制为1000,减去1为111,与运算为0,注意&的优先级低于==,所以写成(i&(i-1))==0

    #include <bits/stdc++.h>
    using namespace std; 
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);
    	int l,r;
    	cin>>l>>r; 
    	int ans=0;
    	for(int i=l;i<=r;i++){
    	if((i&(i-1))==0)ans++; 
    	}
        cout<<ans;
    	return 0;
    }
    
    • 0
      @ 2026-3-12 19:16:27

      正确的写法是位运算~~ 左移即可

      #include <bits/stdc++.h>
      using namespace std;
      #define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
      typedef long long ll; ll n;ll cntl,cnta;const ll N=1e3+5; 
      ll a[N]; ll T=1;
      void solve(){
      	ll l,r;cin>>l>>r;
      	ll x=1;
      	while(x<=r){
      		if(x>=l)cntl++;
      		x=x<<1;
      	}
      	 cout<<cntl;
      }
      int main(){
      	faster;
      	while(T--)solve();
      	return 0;
      }
      
      
      • 0
        @ 2025-5-7 20:11:41

        打表+二分即可

        #include <bits/stdc++.h>
        using namespace std;
        #define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
        typedef long long ll; ll n;
        vector<ll>v={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216};
        ll cntl,cnta,ans; 
        
        void solve(){
        	ll r,l;
        	cin>>l>>r;
        	auto pos_l=lower_bound(v.begin(),v.end(),l); 
        	auto pos_r=upper_bound(v.begin(),v.end(),r);
        	ll len=pos_r-pos_l;
        	cout<<len;
        }
        int main(){
        	faster;
        	ll T=1;
        	while(T--)solve();
        	return 0;
        }
        
        • 1

        信息

        ID
        96
        时间
        1000ms
        内存
        256MiB
        难度
        入门
        标签
        递交数
        101
        已通过
        58
        上传者