zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    第八次作业-谈谈Java web学习小结
    第七周作业--可行性研究与程序系统的结构
    第六次作业-数据库连接
    第五次作业-系统实现可能需要用到的技术,及学习相关技术的心得
    第四次作业-软件需求分析过程与需求分类
    第三次作业-前端与后台数据交换问题
    第二次作业-软件工作量估算方法
    第一次作业
    捣鼓一个Ajax请求管理器
    动动手,写个knockout的分页模板
  • 原文地址:https://www.cnblogs.com/planche/p/8613295.html
Copyright © 2011-2022 走看看