题目链接:Problem - 1606B - Codeforces
Berland State University has received a new update for the operating system. Initially it is installed only on the 11-st computer.
Update files should be copied to all nn computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour.
Your task is to find the minimum number of hours required to copy the update files to all nn computers if there are only kk patch cables in Berland State University.
Input
The first line contains a single integer t (1≤t≤105) — the number of test cases.
Each test case consists of a single line that contains two integers n and k (1≤k≤n≤1018) — the number of computers and the number of patch cables.
Output
For each test case print one integer — the minimum number of hours required to copy the update files to all nn computers.
Example
input
Copy
4 8 3 6 6 7 1 1 1
output
Copy
4 3 6 0
Note
Let's consider the test cases of the example:
- n=8, k=3:
- during the first hour, we copy the update files from the computer 1 to the computer 2;
- during the second hour, we copy the update files from the computer 1 to the computer 3, and from the computer 2 to the computer 4;
- during the third hour, we copy the update files from the computer 11 to the computer 5, from the computer 2 to the computer 6, and from the computer 3 to the computer 7;
- during the fourth hour, we copy the update files from the computer 2 to the computer 8.
- n=6, k=6:
- during the first hour, we copy the update files from the computer 1 to the computer 2;
- during the second hour, we copy the update files from the computer 1 to the computer 3, and from the computer 2 to the computer 4;
- during the third hour, we copy the update files from the computer 1 to the computer 5, and from the computer 2 to the computer 6.
- n=7, k=1:
- during the first hour, we copy the update files from the computer 1 to the computer 2;
- during the second hour, we copy the update files from the computer 1 to the computer 3;
- during the third hour, we copy the update files from the computer 1 to the computer 4;
- during the fourth hour, we copy the update files from the computer 4 to the computer 5;
- during the fifth hour, we copy the update files from the computer 4 to the computer 6;
- during the sixth hour, we copy the update files from the computer 3 to the computer 7.
题意:有k根数据线用来传输数据,一开始只有1台电脑有数据,传输一次需要1小时,问最多需要几小时才能让所有电脑都有数据
思路:刚开始有一台电脑,一小时后2台,之后每小时成倍增加,我们可以记录每个时间的电脑总数,然后多余的用除法计算
#include<bits/stdc++.h>
using namespace std;
map<long long, long long> mp;//用来记录有数据的电脑数量
int main(){
int t;
long long n, x;
cin >> t;
while(t--){
cin >> n >> x;
mp.clear();
long long ans = 1;
mp[0] = 1;
if(n == 1){
cout << 0 << endl;
continue;
}
for(ans; ans; ans++){
mp[ans] = mp[ans - 1] * 2;
if(mp[ans] >= n){
break;
}
if(mp[ans] >= x){
break;
}
if(mp[ans] < 0){
break;
}
}
if(mp[ans] >= n){
cout << ans << endl;
}else{
long long sum = ans;
if((n - mp[ans]) % x != 0){
sum++;
}
sum += (n - mp[ans]) / x;
cout << sum << endl;
}
}
return 0;
}