hihocoder-1498-Diligent Robots
#1498 : Diligent Robots
description
There are N jobs to be finished. It takes a robot 1 hour to finish one job.
At the beginning you have only one robot. Luckily a robot may build more robots identical to itself. It takes a robot Q hours to build another robot.
So what is the minimum number of hours to finish N jobs?
Note two or more robots working on the same job or building the same robot won't accelerate the progress.
Entry
The first line contains 2 integers, N and Q.
For 70% of the data, 1 <= N <= 1000000
For 100% of the data, 1 <= N <= 1000000000000, 1 <= Q <= 1000
Export
The minimum number of hours.
- Sample input
-
10 1
- Sample Output
-
5
answer:
The idea of using greedy algorithm.
After the samples are copied to the production needs to be effective, so the robot must be first copied.
There is an idea whether replication while production side, the idea is negative, if n m copy production, obtained is m + (q + 1) * n. If it is replicated together, the resultant is (m + n ) * q, larger than the former.
#include <cstdio> #include <cstdlib> int main() { int q; long long cnt, n, k; while(scanf("%lld %d", &n, &q) != EOF) { cnt = 0; k = 1; while(2*q*k < n) { k *= 2; cnt += q; } cnt += n / k; if(n%k != 0) { cnt += 1; } printf("%lld\n", cnt ); } return 0; }