Easy Scheduling

Eonathan Eostar decided to learn the magic of multiprocessor systems. He has a full binary tree of tasks with height hh. In the beginning, there is only one ready task in the tree — the task in the root. At each moment of time, pp processes choose at most pp ready tasks and perform them. After that, tasks whose parents were performed become ready for the next moment of time. Once the task becomes ready, it stays ready until it is performed.

You shall calculate the smallest number of time moments the system needs to perform all the tasks.

Input

The first line of the input contains the number of tests tt (1≤t≤5⋅1051≤t≤5⋅105). Each of the next tt lines contains the description of a test. A test is described by two integers hh (1≤h≤501≤h≤50) and pp (1≤p≤1041≤p≤104) — the height of the full binary tree and the number of processes. It is guaranteed that all the tests are different.

Output

For each test output one integer on a separate line — the smallest number of time moments the system needs to perform all the tasks.

Example

Input

3
3 1
3 2
10 6

Output

7
4
173

Note

Let us consider the second test from the sample input. There is a full binary tree of height 33 and there are two processes. At the first moment of time, there is only one ready task, 11, and p1p1 performs it. At the second moment of time, there are two ready tasks, 22 and 33, and the processes perform them. At the third moment of time, there are four ready tasks, 44, 55, 66, and 77, and p1p1 performs 66 and p2p2 performs 55. At the fourth moment of time, there are two ready tasks, 44 and 77, and the processes perform them. Thus, the system spends 44 moments of time to perform all the tasks.

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n,m,mx;
void dfs(ll num,ll t,ll res)
{
	if(num==n)
	{
		if(res>0)t++;
		mx=t;
		return;
	}
    //ll s=1<<num,error ,<<运算,只能到30位(也就是说num最大只能为30)
	ll s=pow(2,num);
    s+=res;
	if(s<=m) dfs(num+1,t+1,0);
	else
	{
		
		ll x=s/m;
		dfs(num+1,t+x,s%m);
	}
	return;
}
int main()
{
	ll i,j,k;
	ll x,t;
	while(t--)
	{
		scanf("%lld%lld",&n,&m);
		dfs(0,0,0);
		printf("%lld\n",mx);
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值