Skip to content

Instantly share code, notes, and snippets.

@dsouzadyn
Created May 30, 2017 16:44
Show Gist options
  • Save dsouzadyn/70fc10771183904b1ee29881bd61209e to your computer and use it in GitHub Desktop.
Save dsouzadyn/70fc10771183904b1ee29881bd61209e to your computer and use it in GitHub Desktop.
Odd
#include <bits/stdc++.h>
#define gc getchar_unlocked
using namespace std;
void scanint(unsigned long &x)
{
register unsigned long c = gc();
x = 0;
for(;(c<48 || c>57);c = gc());
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
}
bool isPowerOf2(long int n)
{
return n && (!(n & (n - 1)));
}
long int power(long int x, long int y)
{
long int temp;
if(y == 0)
return 1;
temp = power(x, y/2);
if(y % 2 == 0)
return temp * temp;
else
{
if(y > 0)
return x*temp*temp;
else
return (temp*temp)/x;
}
}
const int tab64[64] = {
63, 0, 58, 1, 59, 47, 53, 2,
60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20,
55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41,
50, 36, 17, 19, 29, 10, 13, 21,
56, 45, 25, 31, 35, 16, 9, 12,
44, 24, 15, 8, 23, 7, 6, 5
};
int log2_64 (unsigned long value)
{
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
value |= value >> 32;
return tab64[((unsigned long)((value - (value >> 1))*0x07EDD5E59A4E28C2)) >> 58];
}
int main(int argc, char const *argv[])
{
unsigned long T;
unsigned long n;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
scanint(T);
while(T--)
{
scanint(n);
while (n--)
{
if(isPowerOf2(n))
{
printf("%d\n", (int)power(2, log2_64(n)));
break;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment