CodeForces - 1578E
AYIT609国庆大作战一
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.
题意:h层完全二叉树,有p个处理器,每个处理器在某时刻只能处理一个节点,只有父节点被处理过,子节点才能被处理,求最少要多久能处理完
题型:思维题
思路:当k层节点个数刚好小于等于处理器个数时,前k层至少要经历k时刻,因为k层之后每个处理器都能不空闲,所以直接用k层之下的节点数除以处理器p向上取整,就是k层之下需要的时刻数,两时刻数相加就是结果,特殊的,没有刚好小于等于节点数的情况,就直接输出层数。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int main()
{
LL t,h,p,n,a,b,c;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&h,&p);
a=(LL)ceil(log2(p));//刚好小于等于处理器个数的层数,又是前a层所需时刻数
if(a>=h)
printf("%lld\n",h);
else
{
b=(LL)pow(2,a)-1//前a层有多少节点
c=(LL)pow(2,h)-1;//总共有多少节点
n=ceil((c-b)*1.0/p)+a;//ceil((c-b)*1.0/p)是除去前a层剩下的节点需要的时刻
printf("%lld\n",n);
}
}
return 0;
}