codeforce round#466(div.2) B. Our Tanya is Crying Out Loud

B. Our Tanya is Crying Out Loud
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Right now she actually isn't. But she will be, if you don't solve this problem.

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

Subtract 1 from x. This operation costs you A coins.
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
inputCopy
9
2
3
1
output
6
inputCopy
5
5
2
20
output
8
inputCopy
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.

  

题目大意:给一个数n,进行两个操作1.将x减一,但是要花费A;2.只有x能被k整除时,才能除以k,但是要花费B.问进行这两个操作后使的n变成1,问最少要花多少

分析:这个两个操作实际上可以写成:(n-n%k)*A(k<=n,当n不能被整除)和B(当n能被整除),但是这题有个坑点,要注意k=1的时候,不管A比B大多少,除k明显是不行的,所以要当k==1,花费=(n-1)*A

详细见代码

#define debug
#include<stdio.h>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<functional>
#include<iomanip>
#include<map>
#include<set>
#define pb push_back
using namespace std;
typedef long long ll;
pair<ll,ll>PLL;
pair<int,ll>Pil;
const int INF = 0x3f3f3f3f;
const double inf=1e8+100;
const ll maxn =1000;
const int N = 1e4+10;
const ll mod=1000007;
ll n,d;
ll A,B,k;

void solve() {
    int i,j,t=1;
//    cin>>t;
    while(t--) {
        ll ans=0;
        cin>>n>>k>>A>>B;
        if(k==1)
            ans=(n-1)*A;
        else {
            while(n!=1) {
                if(k<=n) {
                    if(n%k) {
                        ans+=(n%k)*A;
                        n-=n%k;
                    } else {
                        ans+=min(B,(n-n/k)*A);
                        n/=k;
                    }
                }
                else{
                    ans+=(n-1)*A;
                    n=1;
                }
            //    cout<<ans<<endl;
            }
        }
        cout<<ans<<endl;
    }
}


int main() {
    ios_base::sync_with_stdio(false);
#ifdef debug
    freopen("in.txt", "r", stdin);
//    freopen("out.txt","w",stdout);
#endif
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

  

 本人也是菜鸡,如有写的不好的地方请指出,谢谢

转载于:https://www.cnblogs.com/visualVK/p/8467577.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值