列表

详情


NC14371. Numbers

描述

DreamGrid has a nonnegative integer n, He would like to divide into m nonnegative integers a1,a2,...am and minimizes their bitwise or (i.e:n = a1 + a2 + ... + am and a1 OR a2 OR ... OR am should be as small as possible).

输入描述

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (0 ≤ n < 101000,1 ≤ m < 10100)

It is guaranteed that the sum of n the length of  does not exceed 20000.

输出描述

For each test case, output an integer denoting the minimum value of their bitwise or.

示例1

输入:

5
3 1
3 2
3 3
10000 5
1244 10

输出:

3
3
1
2000
125

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 102ms, 内存消耗: 6964K, 提交时间: 2021-11-18 15:01:02

T = int(input())
for _ in range(T):
	n,m = map(int,input().split())
	ans=0
	t=1
	while t<n:t<<=1
	while n!=0:
		if (t-1)*m<n:
			tt=min(m,n//t)
			n-=tt*t
			ans|=t
		t>>=1
	print(ans)

上一题