940B Our Tanya is Crying Out Loud

B. Our Tanya is Crying Out Loud
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Right now she actually isn't. But she will be, if you don't solve this problem.

You are given integers nkA and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:

  1. Subtract 1 from x. This operation costs you A coins.
  2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you Bcoins.
What is the minimum amount of coins you have to pay to make  x  equal to  1 ?
Input

The first line contains a single integer n (1 ≤ n ≤ 2·109).

The second line contains a single integer k (1 ≤ k ≤ 2·109).

The third line contains a single integer A (1 ≤ A ≤ 2·109).

The fourth line contains a single integer B (1 ≤ B ≤ 2·109).

Output

Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.

Examples
input
Copy
9
2
3
1
output
Copy
6
input
Copy
5
5
2
20
output
Copy
8
input
Copy
19
3
4
2
output
Copy
12
Note

In the first testcase, the optimal strategy is as follows:

  • Subtract 1 from x (9 → 8) paying 3 coins.
  • Divide x by 2 (8 → 4) paying 1 coin.
  • Divide x by 2 (4 → 2) paying 1 coin.
  • Divide x by 2 (2 → 1) paying 1 coin.

The total cost is 6 coins.

In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.



The meaning of the question: input a n, k, A, B, you need to change n into 1 through two operations, one is to subtract 1 each time, which needs to cost A coins, if the current n is a multiple of K, n It can also be divided by k, which costs B coins. To output the least cost, let n become 1.


Solution: At first, I simulated greedy. If n is a multiple of k, compare the cost of dividing by k, which is the same as the cost of subtracting 1 from n to n/k one by one. Take the smaller cost, not the cost of k Multiples, just subtract one by one, the idea is correct, but it will time out, because it is not a multiple of k, in the case of large data, one by one reduction will time out, and then optimize, we need to compare the cost when n is k When it is a multiple of , so when it is not a multiple, do not reduce it one by one, and reduce it to n at one time, which is a multiple of k. n-=n%k. The time complexity is greatly reduced. The first simulation of the fourth group of data takes 14 seconds, and then the fourth group of data is optimized for 0 seconds.

#include<bits/stdc++.h>
using namespace std;
intmain()
{
    long long n,k,a,b,ans=0;
    cin>>n>>k>>a>>b;
//    double s=clock();
    while(n>1)
    {
        ans+=n%k*a;
        n-=n%k;
        if((n-n/k)*a<b)
              yrs+=(n-1)*y,n=1;
        else ans + = b, n = n / k;
    }
//    double t=clock();
//    cout<<(t-s)/1000<<"毫秒"<<endl;
    cout<<ans<<endl;
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685299&siteId=291194637