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 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.