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

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

B. 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 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:

  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的情况。

#include
using namespace std;int main(){ long long N,k,A,B; scanf("%lld%lld%lld%lld",&N,&k,&A,&B); long long ans=0; if(k==1) { printf("%lld\n",A*(N-1)); return 0; } while(N>=k) { if(N%k==0) { long long next=N/k; if((N-next)*A>B) ans+=B; else ans+=(N-next)*A; N=next; } else { long long t=N/k; long long temp=N-t*k; ans+=A*temp; N=t*k; } } if(N!=1)//N
View Code

 

 

转载于:https://www.cnblogs.com/ACRykl/p/8471743.html

你可能感兴趣的文章
Java 反射工具类
查看>>
Java继承关系中,父类方法使用实例变量和调用实例方法的探究
查看>>
csdn账号密码
查看>>
AWS 配置 AutoScaling 实现高可用的弹性计算服务
查看>>
Ubuntu 16.04配置hive mysql db元数据
查看>>
Struts2 中的值栈的理解
查看>>
linux下IPTABLES配置详解
查看>>
使用Spring Mail 模块连接STMP服务器发送邮件
查看>>
Tomcat 下面使用软连接指向真实的上传文件夹
查看>>
批量修改日期,按日期生成文件
查看>>
Git的后悔药——重置 签出 撤消 取消跟踪
查看>>
mysql 5.5.x 解压版 安装
查看>>
设计模式之装饰模式
查看>>
64位Windows 2008/7下配置IIS+PHP出现404.17错误的解决办法
查看>>
mysql 安装使用过程中的问题与解决
查看>>
Java面试题之四 (转)
查看>>
并发编程之ThreadLocal、Volatile、synchronized、Atomic关键字扫盲
查看>>
yii2控制台执行
查看>>
Spring Boot Scheduler 定时任务 Quartz 集群 Culster 2种实现方式
查看>>
关于织梦自带的函数ShowMsg()函数的使用方法
查看>>