Problem:


Score : 100 points

Problem Statement

A professor invented Cookie Breeding Machine for his students who like cookies very much. 

When one cookie with the taste of x is put into the machine and a non-negative integer y less than or equal to 127 is input on the machine,
        it consumes the cookie and generates two cookies with the taste of y and (x XOR y). 

Here, XOR represents Bitwise Exclusive OR. 

At first, there is only one cookie and the taste of it is D . 

Find the maximum value of the sum of the taste of the exactly N cookies generated after the following operation is conducted N-1 times. 

Put one of the cookies into the machine. 

Input a non-negative integer less than or equal to 127 on the machine. 

Constraints

1 \leq T \leq 1000

1 \leq N_t \leq 1000 (1 \leq t \leq T)

1 \leq D_t \leq 127 (1 \leq t \leq T)

Input

The input is given from Standard Input in the following format:

T
N_1 D_1
:
N_T D_T

            The input consists of multiple test cases. An Integer T that represents the number of test cases is given on line 1. 

            Each test case is given on the next T lines. 

            In the t-th test case ( 1 \leq t \leq T ),
            N_t that represents the number of cookies generated through the operations and D_t that represents the taste of the initial cookie are given separated by space. 

Output

For each test case, print the maximum value of the sum of the taste of the N cookies generated through the operations on one line. 

Sample Input 1

3
3 1
4 108
1 10

Sample Output 1

255
400
10

        On the 1st test case, if the machine is used as follows, three cookies with the taste of 61, 95 and 99 are generated.
        Since the sum of these values is maximum among all possible ways, the answer is 255.
        

Put the cookie with the taste of 1 and input an integer 60 on the machine, lose the cookie and get two cookies with the taste of 60 and 61. 

Put the cookie with the taste of 60 and input an integer 99 on the machine, lose the cookie and get two cookies with the taste of 99 and 95. 

        On the 3rd test case, the machine may not be used.
    



Sample input:
3
3 1
4 108
1 10


Sample output:
255
400
10

Submission status: Wrong Answer

Solution code:
N,T=0,0
for i in range(int(input())):
  N,T=map(int,input().split())
  if N&1:
    T^=127
  print(T+(N-1)*127)

Submission status: Wrong Answer

Solution code:
q = int(input())
for _ in range(q):
  n,d = map(int, input().split())
  print(127*(n-1) + d^(127*((n-1)&1)))

Submission status: Wrong Answer

Solution code:
T = int(input())
ts = [tuple(map(int,input().split())) for i in range(T)]

def solve(n,d):
    if n%2:
        return d + 127*(n-1)
    else:
        return d^127 + 127*(n-1)

for n,d in ts:
    print(solve(n,d))

Submission status: Wrong Answer

Solution code:
T = int(input())
ts = [tuple(map(int,input().split())) for i in range(T)]

def solve(n,d):
    if n%2:
        return d + 127*(n-1)
    else:
        tmp = 0
        for a in range(128):
            tmp = max(tmp, a + a^d)
        return tmp - d + 127*(n-2)

for n,d in ts:
    print(solve(n,d))

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
T = int(input())

for i in range(T):
    N, D = map(int, input().split())
    ans = D
    h = []
    heappush(h,D)
    for _ in range(N - 1):
        k = heappop(h)
        z = bin(k)[2:].zfill(7)
        t = int(z,2)
        heappush(h,t)
        ans += (127 + t - k)
    print(ans)

Submission status: Wrong Answer

Solution code:
for i in[0]*input():n,d=map(int,raw_input().split());print(n-n%2)*127^d

Submission status: Time Limit Exceeded

Solution code:
T = int(input())
for _ in range(T):
    N,D = map(int,input().split())
    cookie = [D]
    while len(cookie)<N:
        cookie.sort()
        cookie = cookie[1:] + [127-cookie[0],127]
    print(sum(cookie))

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
T = int(input())
A = []
for i in range(T):
    N, D = map(int, input().split())
    ans = D
    h = []
    heappush(h,D)
    for _ in range(N - 1):
        k = heappop(h)
        z = bin(k)[2:].zfill(7)
        u = map(lambda x: '0' if x == '1' else '1', z)
        t = int(''.join(u),2)
        heappush(h,t)
        ans += (127 + t - k)
    A.append(ans)
for i in range(len(A)):
    print(A[i])

Submission status: Time Limit Exceeded

Solution code:
from heapq import heappush, heappop
T = int(input())

for i in range(T):
    N, D = map(int, input().split())
    ans = D
    h = []
    heappush(h,D)
    for _ in range(N - 1):
        k = heappop(h)
        z = bin(k)[2:].zfill(7)
        u = map(lambda x: '0' if x == '1' else '1', z)
        t = int(''.join(u),2)
        heappush(h,t)
        ans += (127 + t - k)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
r=range(128)
def f():v=min(i for i in r if D[i]);D[v]-=1;D[127-v]+=1;D[127]+=1
for i in[0]*input():n,d=map(int,raw_input().split());D=[0]*128;D[d]=1;exec("f();"*~-n);print sum(i*D[i]for i in r)

Submission status: Accepted

Solution code:
N=int(input())
for i in range(N):
    A,B=map(int,input().split())
    if A%2==1:
        ans=B+127*(A-1)
    else:
        ans=127*A-B
    print(ans)

Submission status: Accepted

Solution code:
N,T=0,0
for i in range(int(input())):
  N,T=map(int,input().split())
  if N&1==0:
    T^=127
  print(T+(N-1)*127)

Submission status: Accepted

Solution code:
def solve(N,D):
    N-=1
    if  (N)%2==0:
        return 127*N+D
    else:
        return 127*N+(127-D)
T=int(input())
for _ in range(T):
    a,b=map(int,input().split())
    print(solve(a,b))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

"""
全体のxorが不変量。
N-1数が127のときに等号
"""

T = int(input())
ND = [[int(x) for x in input().split()] for _ in range(T)]

answer = []
for N,D in ND:
    x = 127 * (N-1)
    if N&1:
        x += D
    else:
        x += 127^D
    answer.append(x)

print('\n'.join(map(str,answer)))

Submission status: Accepted

Solution code:
def inpl(): return list(map(int, input().split()))

for _ in range(int(input())):
    N, D = inpl()
    ans = 0
    for i in range(7):
        D, m = divmod(D, 2)
        ans += (N-(N%2!=m))*(2**i)
    print(ans)

Submission status: Accepted

Solution code:
for i in range(int(input())):
    n,d=map(int,input().split())
    print([(n-1)*127+d,n*127-d][n%2==0])

Submission status: Accepted

Solution code:
T = int(input())
ts = [tuple(map(int,input().split())) for i in range(T)]

def solve(n,d):
    if n%2:
        return d + 127*(n-1)
    else:
        return 127*n - d

for n,d in ts:
    print(solve(n,d))

Submission status: Accepted

Solution code:
T = int(input())
for _ in range(T):
    N,D = map(int,input().split())
    if N%2==0:
        print(N*127-D)
    else:
        print((N-1)*127+D)

Submission status: Accepted

Solution code:
n = int(input())
for _ in range(n):
	i,j = (int(k) for k in input().split())
	if i%2==0: print(127*i-j)
	else: print(127*(i-1)+j)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
T = int(input())
A = []
for i in range(T):
    N, D = map(int, input().split())
    ans = D
    h = []
    heappush(h,D)
    for _ in range(N - 1):
        k = heappop(h)
        t = 127 ^ k
        heappush(h,t)
        ans += (127 + t - k)
    A.append(ans)
for i in range(len(A)):
    print(A[i])

Submission status: Accepted

Solution code:
def solve(N_t, D_t):
    ans = D_t
    for i in range(N_t - 1):
        ans -= D_t
        ans += 127
        D_t = 127 ^ D_t
        ans += D_t
    return ans

T = int(input())
for i in range(T):
    N_t, D_t = map(int, input().split())
    print(solve(N_t, D_t))

Submission status: Accepted

Solution code:
for i in[0]*input():n,d=map(int,raw_input().split());print~-n*127+(127*(n+1&1)^d)

Submission status: Accepted

Solution code:
t=int(input())
for i in range(t):
    n,d=map(int,input().split())
    ans=127*(n-1)
    if n%2==1:
        ans+=d
    else:
        ans+=127^d
    print(ans)

Submission status: Accepted

Solution code:
def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    t = read()
    for _ in range(t):
        n, d = reads()
        
        if n == 1:
            print(d)
        elif n % 2 == 0:
            print(127*(n-1) + (d^127))
        else:
            print(127*(n-1) + d)
        
        # 3,  1 [1] -> [126, 127] -> [127, 1, 127]
        # 4,108 [108] -> [19, 127] -> [127, 127, 108] -> [127, 127, 127, 19]

main()

Submission status: Accepted

Solution code:
T = int(input())
for _ in range(T):
    n, d = map(int, input().split())
    print(127*(n-1) + (d if n%2==1 else 127-d))

Submission status: Accepted

Solution code:
T = int(raw_input())
N_list = list()
D_list = list()
for t in range(T):
    N, D = map(int, raw_input().split())
    N_list.append(N)
    D_list.append(D)
    
for t in range(T):
    N = N_list[t]
    D = D_list[t]
    if N % 2 == 0:
        res = N * 127 - D
    else:
        res = 127*(N-1) + D
    print res

Submission status: Accepted

Solution code:
f = [[], [[1, 0], [0, 1]]]
dp = [[], [0, 1]]
for i in xrange(1, 1010):
    a = []
    d = []
    for j in xrange(2):
        b = f[i][j]
        g = [0] * (i + 2)
        for k, l in enumerate(b):
            if not l:
                continue
            if k > 0:
                g[k] = i
            if k < i:
                g[k+2] = i
        a.append(g)
        t = 0
        for k, l in enumerate(g):
            if l:
                t = k
        d.append(t)
    f.append(a)
    dp.append(d)
t = int(raw_input())
for _ in xrange(t):
    n, d = map(int, raw_input().split())
    s = 0
    for i in xrange(7):
        s += dp[n][d>>i&1] << i
    print s

Submission status: Accepted

Solution code:
def solve(): 
	T = int(raw_input())
	for i in xrange(T):
		N,D = map(int,raw_input().split())
		print 127*(N-1) + (D if N % 2 == 1 else D^127)
solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

T = int(raw_input())
for loop in xrange(T):
    N, D = map(int, raw_input().split())
    ans = 0
    for i in xrange(N - 2):
        ans += 127
        D ^= 127

    mx = 0
    if N > 1:
        for i in xrange(1, 128):
            mx = max(mx, i + (D^i))
        print ans + mx
    else:
        print D