目录

now112544f: 汉堡猪猪分糖果

目录

now112544f: 汉堡猪猪分糖果

贪心

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import sys
input = lambda: sys.stdin.readline().rstrip()

def II(base=10):
    return int(input(),base)

def LI():
    return list(map(int,input()))

def LII():
    return list(map(int,input().split()))

def main():
    n,m = LII()
    N = n.bit_length()
    res = 0
    for i in range(N-1,-1,-1):
        x = 1<<i
        if x*m<=n:
            res |= x
            n -= x*m
        elif (x-1)*m<n:
            r = n-(x-1)*m
            c = (r-1)//x+1
            n -= c*x
    return res

for _ in range(II()):
    print(main())

427 ms