1 条题解

  • 0
    @ 2025-4-14 18:43:49

    C :

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    typedef long long LL;
    LL x,y;
    LL s;
    int main(){
    	while(~scanf("%lld",&x)){
    		s=(LL)(log(x+1)/log(2.0));
    		x=x-pow(2,s)+1;
    		if(x==0)
    			printf("%lld\n",(LL)(pow(2,s)-1));
    		else
    			printf("%lld\n",2*x-1);
    	}
    	return 0;
    } 
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        ll n;
        while(~scanf("%lld",&n)){
            ll len=0;
            ll ans=0;
            ll data=1;
            for (ll i=0;i<63;i++){
                if ((data<<i)>n){
                    len=i-1;
                    break;
                }
            }
            ans=(n-(data<<len));
            printf("%lld\n",(ans<<data)+data);
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    3354
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者