Our Tanya is Crying Out Loud
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

题目的意思是给你两种操作,一种是每次减1消耗a元,一种是每次除以k每次消耗b元。使n变成1的最小消耗。

在每次没有达到k的倍数前,只能减一,达到后判断下消耗是减去还是除以小,选小的。

#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 100010
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
int main() {
ll n,k,a,b;
while( cin >> n >> k >> a >> b ) {
ll ans = ;
if( k == ) {
cout << ( n - ) * a << endl;
continue;
}
while( n != ) {
if( n % k == ) {
ans += min( ( n - n / k ) * a, b );
n /= k;
} else if( n > k ) {
ans += ( n % k ) * a;
n -= n % k;
} else {
ans += ( n - ) * a;
n = ;
}
}
cout << ans << endl;
}
return ;
}

最新文章

  1. nginx:413 Request Entity Too Large 及 修改 PHP上传文件大小配置
  2. Linux进程间通信(一): 信号 signal()、sigaction()
  3. C# 异常:从作用域“”引用了“FiasHostApp.Entity.DBEntity.FIAS_RM_v1.ITraNetMgrUnitBaseInfoRecord”类型的变量“w”,但该变量未定义
  4. N900快捷键
  5. 2016 08 27 印刷菜单增加sql语句
  6. VTK初学一,e_Triangle三角形的绘制
  7. Freemarker使用入门
  8. WeX5之xid相关API
  9. jQuery之DOM操作
  10. 【转】android: 长按删除listview的item
  11. Spring Boot 探索系列 - 自动化配置篇
  12. Performing User-Managed Database-18.4、Restoring Datafiles and Archived Redo Logs
  13. jquery 精度计算代码,指定精确小数位
  14. [命令行] curl查询公网出口IP
  15. 狙杀ES6之开光篇
  16. 浮点型和BigDecimal的使用
  17. python 时间段的随机日期输出
  18. 一、linux扩展
  19. 51nod1016
  20. ACM-ICPC 2018 南京赛区网络预赛 B The writing on the wall(思维)

热门文章

  1. java在src/test/resourse下读取properties文件
  2. ansible批量管理服务 下
  3. UR机器人通信--上位机通信(python)
  4. Hadoop学习(5)-zookeeper的安装和命令行,java操作
  5. 佳木斯集训Day7
  6. 使用sar进行性能分析
  7. Linux文件及目录管理
  8. 8.8 day29 异常处理 UDP通信
  9. Kafka 系列(四)—— Kafka 消费者详解
  10. SpringBoot 缓存模块