博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF940B Our Tanya is Crying Out Loud
阅读量:6443 次
发布时间:2019-06-23

本文共 2153 字,大约阅读时间需要 7 分钟。

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#include
#include
#include
#include
#include
#include
#include
#define maxn 100010#define debug(a) cout << #a << " " << a << endlusing namespace std;typedef long long ll;int main() { ll n,k,a,b; while( cin >> n >> k >> a >> b ) { ll ans = 0; if( k == 1 ) { cout << ( n - 1 ) * a << endl; continue; } while( n != 1 ) { if( n % k == 0 ) { ans += min( ( n - n / k ) * a, b ); n /= k; } else if( n > k ) { ans += ( n % k ) * a; n -= n % k; } else { ans += ( n - 1 ) * a; n = 1; } } cout << ans << endl; } return 0;}

 

转载于:https://www.cnblogs.com/l609929321/p/8469391.html

你可能感兴趣的文章
Linux命令行得到系统IP
查看>>
SQL Server索引的维护 - 索引碎片、填充因子 <第三篇>
查看>>
python类型转换、数值操作(收藏)
查看>>
mysql delimiter
查看>>
关于C#静态构造函数的几点说明
查看>>
理解C# 4 dynamic(4) – 让人惊艳的Clay
查看>>
管理-职业化沟通
查看>>
angular之$compile
查看>>
SQL中Truncate的用法
查看>>
一键安装docker-ce
查看>>
彻底理解Netty,这一篇文章就够了
查看>>
极光开发者沙龙 JIGUANG MEETUP —— 移动应用性能优化实践
查看>>
最新的CocoaPods安装步骤 pod install/pod update更新慢等问题
查看>>
高并发下的一些问题
查看>>
如何为Django添加中文搜索服务
查看>>
Spring Cloud Config 统一配置中心
查看>>
Java获取文本文件字符编码的两种方法
查看>>
js数据类型只string,object
查看>>
android httpClient(https/http)的优化构建方式二
查看>>
架设用Webservice实现文件上传功能CentOS服务器(一)--Tomcat
查看>>