• B. Our Tanya is Crying Out Loud


    http://codeforces.com/problemset/problem/940/B

    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
    6
    input
    Copy
    5
    5
    2
    20
    output
    8
    input
    Copy
    19
    3
    4
    2
    output
    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.

     简单题,但记得特判K=1

    // 去吧!皮卡丘! 把AC带回来!
    //      へ     /|
    //   /\7    ∠_/
    //   / │   / /
    //  │ Z _,< /   /`ヽ
    //  │     ヽ   /  〉
    //  Y     `  /  /
    //  イ● 、 ●  ⊂⊃〈  /
    //  ()  へ    | \〈
    //   >ー 、_  ィ  │ //
    //   / へ   / ノ<| \\
    //   ヽ_ノ  (_/  │//
    //    7       |/
    //    >―r ̄ ̄`ー―_
    //**************************************
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define inf 2147483647
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    #define ri register int
    template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
    template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
    template <class T> inline T min(T a, T b, T c, T d) {
      return min(min(a, b), min(c, d));
    }
    template <class T> inline T max(T a, T b, T c, T d) {
      return max(max(a, b), max(c, d));
    }
    #define scanf1(x) scanf("%d", &x)
    #define scanf2(x, y) scanf("%d%d", &x, &y)
    #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
    #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
    #define pi acos(-1)
    #define me(x, y) memset(x, y, sizeof(x));
    #define For(i, a, b) for (int i = a; i <= b; i++)
    #define FFor(i, a, b) for (int i = a; i >= b; i--)
    #define bug printf("***********
    ");
    #define mp make_pair
    #define pb push_back
    const int maxn = 3e5 + 10;
    const int maxx = 1e6 + 10;
    // name*******************************
    ll n, k, A, B;
    ll ans = 0;
    // function******************************
    
    //***************************************
    int main() {
      cin >> n >> k >> A >> B;
      if(k==1){
        cout<<(n-1)*A;
        return 0;
      }  
      while (n) {
        if (n < k) {
          ans += (n - 1) * A;
          break;
        }
        int t = n / k;
        ans += (n - k * t) * A + min((k - 1) * A * t, B);
        n = t;
      }
      cout << ans;
    
      return 0;
    }
  • 相关阅读:
    C#轻量级企业事务
    扩展方法
    JDK Environment Variable And Change default JDK
    AsyncTask简单获取网络图片的例子
    mysql基础
    Oracle基础操作
    java 中 colkection集合、迭代器、增强for、泛型
    centos7 解决 mysql_connect()不支持请检查mysql模块是否正确加载
    python 操作MySQL避坑1064
    面向对象学习
  • 原文地址:https://www.cnblogs.com/planche/p/8613295.html
Copyright © 2020-2023  润新知