Created
February 23, 2020 20:23
-
-
Save ZenulAbidin/7a05c88b8cd6957a57b174bb9d0a579d to your computer and use it in GitHub Desktop.
Day 29: Bitwise AND
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
if __name__ == '__main__': | |
t = int(input()) | |
for t_itr in range(t): | |
nk = input().split() | |
n = int(nk[0]) | |
k = int(nk[1]) | |
maxab = 0 | |
ab = (0, 0) | |
mk = k | |
mk |= mk >> 1 | |
mk |= mk >> 2 | |
mk |= mk >> 4 | |
mk |= mk >> 8 | |
mk |= mk >> 16 | |
mk += 1 | |
msbk = mk >> 1 | |
if msbk == k: | |
msbk = int(msbk/2) | |
for b in range(1, min(2*k, n)+1): | |
for a in range(msbk, b): | |
aandb = a & b | |
if aandb > maxab and aandb < k: | |
ab = (a, b) | |
maxab = aandb | |
print(maxab) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment