Eonathan Eostar decided to learn the magic of multiprocessor systems. He has a full binary tree of tasks with height h. In the beginning, there is only one ready task in the tree — the task in the root. At each moment of time, p processes choose at most p 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 t (1≤t≤5⋅105). Each of the next t lines contains the description of a test. A test is described by two integers h (1≤h≤50) and p (1≤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 3 and there are two processes. At the first moment of time, there is only one ready task, 1, and p1 performs it. At the second moment of time, there are two ready tasks, 2 and 3, and the processes perform them. At the third moment of time, there are four ready tasks, 4, 5, 6, and 7, and p1 performs 6 and p2 performs 5. At the fourth moment of time, there are two ready tasks, 4 and 7, and the processes perform them. Thus, the system spends 4 moments of time to perform all the tasks.
题意
Eonathan Eostar决定学习多处理器系统的魔力。他有一个高度为h的任务的完整二叉树。开始时,树中只有一个就绪的任务——根任务。在每个时刻,p个进程最多选择p个就绪任务并执行它们。在那之后,父母完成的任务为下一个时刻做好了准备。一旦任务准备就绪,它将一直保持就绪状态,直到执行。
你应该计算出系统执行所有任务所需的最小时刻数。
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[100];
int main() {
ll i,j,t,w,h,p,z,ans=0;
a[1]=2;
for(i=2; i<=50; i++)
a[i]=a[i-1]*2;
scanf("%lld",&w);
while(w--) {
scanf("%lld %lld",&h,&p);
if(p>a[h]-1) p=a[h]-1;
t=1,z=1;
while(t<=p) t*=2,z++;
z--;
ans=a[h]-a[z];
ans=(ans+p-1)/p;
ans+=z;
printf("%lld\n",ans);
}
return 0;
}