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("***********\n");
#define mp make_pair
#define pb push_back
const int maxn = 3e5 + ;
const int maxx = 1e6 + ;
// name*******************************
ll n, k, A, B;
ll ans = ;
// function****************************** //***************************************
int main() {
cin >> n >> k >> A >> B;
if(k==){
cout<<(n-)*A;
return ;
}
while (n) {
if (n < k) {
ans += (n - ) * A;
break;
}
int t = n / k;
ans += (n - k * t) * A + min((k - ) * A * t, B);
n = t;
}
cout << ans; return ;
}

B. Our Tanya is Crying Out Loud的更多相关文章

  1. 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 input ...

  2. Codeforces Round #466 (Div. 2) B. Our Tanya is Crying Out Loud[将n变为1,有两种方式,求最小花费/贪心]

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

  3. CF940B Our Tanya is Crying Out Loud

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

  4. 940B Our Tanya is Crying Out Loud

    传送门 题目大意 给你n,k,A,B四个数,x=n,有两种操作: 1.将x-1,需支付A个金币 2.将x÷k,需支付B个金币,当且仅当k能整除x时可进行此操作 问将x修改为1至少要花几个金币 分析 模 ...

  5. 「日常训练」Our Tanya is Crying Out Loud (CFR466D2B)

    题意(Codeforces 940B) 对一个数字$x$,你有两个决策:花费$A$减一.或花费$B$除以$k$(但必须可以除尽).问使之到$1$的最少花费. 分析 贼鸡儿简单,但我花式犯蠢……如果除不 ...

  6. Codeforces Round #466 (Div. 2)

    所有的题目都可以在CodeForces上查看 中间看起来有很多场比赛我没有写了 其实是因为有题目没改完 因为我不想改,所以就没有写了(大部分题目还是改完了的) 我还是觉得如果是打了的比赛就一场一场写比 ...

  7. Codeforces Round #466 (Div. 2) 题解

    人生中第三次\(CF\)... 考试中切了\(A\)~\(E\) \(F\)题会做没时间写 题解 A:Points on the line 题意 给定一个数列,删最小的数,使最大差不大于一个定值 So ...

  8. Codeforces Round #466 (Div. 2) Solution

    从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...

  9. 【codeforces】【比赛题解】#940 CF Round #466 (Div. 2)

    人生的大起大落莫过如此,下一场我一定要回紫. [A]Points on the line 题意: 一个直线上有\(n\)个点,要求去掉最少的点,使得最远两点距离不超过\(d\). 题解: 暴力两重fo ...

随机推荐

  1. Python手写模拟单向链表对象,栈对象和树

    单向链表: class error(Exception): def __init__(self,msg): super(error,self).__init__() self.msg=msg def ...

  2. 【代码笔记】iOS-获取现在的日历时间

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  3. Pig filter用法举例

    filter:过滤数据,只有符合特定条件的数据才会被保留下来,然后进入下一个数据流.   1)等值比较 filter data by $0 == 1 filter data by $0 != 1   ...

  4. Redis redis-trib集群配置

    redis文档:http://doc.redisfans.com/ 参考:https://www.cnblogs.com/wuxl360/p/5920330.html http://www.cnblo ...

  5. Angularjs controller之间的通信

    刚刚看了网上的一些关于控制器之间的通信:然后结合自己项目做了一些,这里主要做的是二个同级之间的controller通信. Html: <html> <script src=" ...

  6. EJB系列 - EJB基础知识

    本人博客文章网址:https://www.peretang.com/basic-knowledge-of-ejb/ 什么是EJB 可移植的, 可重用的, 可伸缩的业务应用程序的平台 为什么选择EJB ...

  7. ReadWriteLock ReentrantReadWriteLock

    ReadWriteLock管理一组锁,一个是只读的锁,一个是写锁.读锁可以在没有写锁的时候被多个线程同时持有,写锁是独占的. 所有读写锁的实现必须确保写操作对读操作的内存影响.换句话说,一个获得了读锁 ...

  8. EXC_BAD_ACCESS错误

    EXC_BAD_ACCESS错误 一直都是使用ARC开发,咋就莫名其妙的出现这种EXC_BAD_ACCESS错误. 一直都是是怀疑在block中有着特殊处理,导致了使用时出现了这种错误,查找了好久.. ...

  9. UNIX高级环境编程(8)进程环境(Process Environment)- 进程的启动和退出、内存布局、环境变量列表

    在学习进程控制相关知识之前,我们需要了解一个单进程的运行环境. 本章我们将了解一下的内容: 程序运行时,main函数是如何被调用的: 命令行参数是如何被传入到程序中的: 一个典型的内存布局是怎样的: ...

  10. MacOS(苹果电脑&苹果系统)连接京瓷300i 打印机

    前往京瓷官网下载打印机驱动: http://www.kyoceradocumentsolutions.com.cn/support/mfp/download/taskalfa300i.html 驱动安 ...