Timus 1131. Copying 数学公式的构造

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值