P15300 [GeeksforGeeks] Optimal walk3752
普及-
题目描述

Given a point on x axis, (N,0), you are standing at origin and you can only move on x-axis. Lets say you are at (x,0), at each step you can either move one step forward ie (x+1,0) or one step backwards (x-1,0) or make a jump to double your current position (2*x,0). One step movement costs you A while a jump costs you B. If your initial position is (0,0) , find the minimum cost to reach (N,0) .
Example 1:
Input: N = 7, A = 1, B = 100
Output: 7
Explaination: Move forward to (N, 0) one
unit at each step.
Example 2:
Input: N = 8, A = 2, B = 4
Output: 12
Explaination: The optimal movement is to
1, 2, 3, 4, 8.
Your Task:
You do not need to read input or print anything. Your task is to complete the function optimalWalk() which takes N, A and B as input parameters and returns the minimum cost to go to (N, 0) from (0, 0).
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A, B ≤ 10^{9}

输入格式

见题面

输出格式

见题面

样例输入
N = 7, A = 1, B = 100
样例输出
7