CodeChef Starters 31 Division 3 (Rated) The Unchanging OR Problem Code: UNCHANGEDOR Solution




Problem Link:   https://www.codechef.com/START31C/problems/UNCHANGEDOR

 Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n,x,t,cn,a,s,r;
    cin>>t;
    while(t--)
    {
        x=0;
        cin>>n;
        cn=n;
        if(n==2)
            cout<<"0"<<endl;
        else if(n==3||n==4)
            cout<<"1"<<endl;
        else
        {
            for(int i=1; i<=50; i++)
            {
                s=pow(2,i);
                x++;
                if(s>n)
                    break;
            }
            x--;
            a=cn-(pow(2,x));
            r=(pow(2,x)-x-1);
            if(a==0)
            {
                cout<<r<<endl;
            }
            else
            {
                cout<<r+a<<endl;
            }
        }
    }
}

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.