• Our Tanya is Crying Out Loud


    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 B coins.
    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.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <unordered_set>
    #include <unordered_map>
    //#include <xfunctional>
    #define ll long long
    #define mod 1000000007
    using namespace std;
    int dir[4][2] = { { 0,1 },{ 0,-1 },{ -1,0 },{ 1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    
    int main()
    {
        ll n, k, A, B;
        ll res=0;
        cin >> n >> k >> A >> B;
        if (k == 1)
            cout << (n - 1)*A;
        else
        {
            while (n != 1)
            {
                if (n < k)
                {
                    res += (n - 1)*A;
                    n = 1;
                }
                else
                {
                    if (n%k > 0)
                    {
                        res += (n%k)*A;
                        n -= n%k;
                    }
                    if ((n - n / k)*A > B)
                    {
                        res += B;
                        n /= k;
                    }
                    else
                    {
                        res += (n - n / k)*A;
                        n /= k;
                    }
                }
            }
            cout << res;
        }
        return 0;
    }
  • 相关阅读:
    改变GMF应用程序画布的布局
    Eclipse 3.2下载最多的国家和地区
    让输出的Plugin文件名里包含当前时间
    把SWT包装成Plugin需要修改的地方
    在程序里隐藏但利用Resource Navigator
    GMF应用程序设置背景图片
    给GMF应用程序添加自定义Action
    Graphical Modeling Framework简介
    GMF常见问题
    EReference的containment和container属性
  • 原文地址:https://www.cnblogs.com/dealer/p/12442701.html
Copyright © 2020-2023  润新知