A new educating program was received by the kindergarten. Of course, children have discovered it immediately and want to play with it as soon as possible. In order to let them do it the program has to be copied to all the
Ncomputers that the kindergarten had bought just before the default of 1998. At the moment the program is installed only on one computer. Other computers do not have floppy drives and are not connected with a local network. The only way to transfer information from one computer to another is to copy it using a null-modem cable (a cable that connects two computers directly). So, if the program is installed on a computer, it can be copied to some other (but only one!) computer within an hour. There are only
Knull-modem cables in the kindergarten. Your task is to estimate the minimal time necessary for copying the program to all the computers in the kindergarten.
Input
The only input line contains two integers separated with a space:
Nand
K(1 ≤
N≤ 10
9; 1 ≤
K≤ 10
9).
Output
You are to output the minimal time (in hours) necessary for copying of the program to all the computers.
Sample
input | output |
---|---|
8 3 | 4 |
这种题目一般都无法模拟它的工作过程的了,否则铁定超时了。
所以这样的题目必定是利用数学知识构造公式。
注意观察:
1 有充裕的k可以使用的时候,每个小时可以拷贝的电脑都是呈倍数增长的,得到数列:1 2 4 8 16 32...
2 当k不够用的时候就是每个小时增加k台电脑:k 2k 3k 4k...
这样就可以构造公式了,看程序如何实现:
最终的时间效率是O(lgn)
#include <iostream>
using namespace std;
void Copying1131()
{
long long n, k;
cin>>n>>k;
int t = 0;
long long val = 1;
while (val < k && val < n)
{
t++;
val <<= 1;
}
if (val >= n) cout<<t;
else
{
n -= val;
t += (int)(n / k);
if (n % k) t++;
cout<<t;
}
}
int main()
{
Copying1131();
return 0;
}