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 theNcomputers 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 onlyKnull-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:NandK(1 ≤N≤ 109; 1 ≤K≤ 109).

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



您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

在处理图片保存失败并提示 `IOException caught while copying` 时,可从多方面着手解决。 ### 检查文件权限 确保程序对目标保存目录有写入权限。若权限不足,会引发 `IOException`。在不同操作系统中,设置文件权限的方式有所不同。 - **Windows**:可以右键点击目标文件夹,选择“属性”,在“安全”选项卡中修改权限,确保运行程序的用户账户有写入权限。 - **Linux**:使用 `chmod` 命令修改目录权限,例如 `chmod 777 target_directory` 可赋予所有用户读写执行权限。 ### 检查文件是否被占用 若目标文件已被其他程序打开或占用,保存操作会失败并抛出异常。关闭可能占用该文件的程序,或更换保存路径。可以通过系统自带的资源监视器或第三方工具查看文件占用情况。 ### 检查磁盘空间 磁盘空间不足会导致文件无法保存。可查看磁盘剩余空间,若空间不足,可清理磁盘或更换保存位置。在 Windows 系统中,右键点击磁盘盘符,选择“属性”查看磁盘使用情况;在 Linux 系统中,使用 `df -h` 命令查看磁盘空间。 ### 异常捕获与日志记录 在代码中捕获 `IOException`,并记录详细的错误信息,有助于定位问题。以下是一个示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ImageCopy { public static void main(String[] args) { File sourceFile = new File("source_image.jpg"); File destinationFile = new File("destination_image.jpg"); try (FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(destinationFile)) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { fos.write(buffer, 0, length); } } catch (IOException e) { System.err.println("IOException caught while copying: " + e.getMessage()); e.printStackTrace(); // 可以在这里添加更多的错误处理逻辑,如重试机制等 } } } ``` ### 网络问题(如果涉及网络传输) 若图片是从网络下载保存,网络不稳定或连接中断会导致保存失败。检查网络连接,确保网络正常,也可以尝试增加重试机制。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值