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;
}
本人也是菜鸡,如有写的不好的地方请指出,谢谢
codeforce round#466(div.2) B. Our Tanya is Crying Out Loud的更多相关文章
- 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 ...
- codeforce round#466(div.2)C. Phone Numbers
C. Phone Numbers time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #466 (Div. 2) E. Cashback
Codeforces Round #466 (Div. 2) E. Cashback(dp + 贪心) 题意: 给一个长度为\(n\)的序列\(a_i\),给出一个整数\(c\) 定义序列中一段长度为 ...
- Codeforces Round #466 (Div. 2) Solution
从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...
- Codeforces Round #466 (Div. 2)
所有的题目都可以在CodeForces上查看 中间看起来有很多场比赛我没有写了 其实是因为有题目没改完 因为我不想改,所以就没有写了(大部分题目还是改完了的) 我还是觉得如果是打了的比赛就一场一场写比 ...
- Codeforces Round #466 (Div. 2) 题解
人生中第三次\(CF\)... 考试中切了\(A\)~\(E\) \(F\)题会做没时间写 题解 A:Points on the line 题意 给定一个数列,删最小的数,使最大差不大于一个定值 So ...
- 【codeforces】【比赛题解】#940 CF Round #466 (Div. 2)
人生的大起大落莫过如此,下一场我一定要回紫. [A]Points on the line 题意: 一个直线上有\(n\)个点,要求去掉最少的点,使得最远两点距离不超过\(d\). 题解: 暴力两重fo ...
- 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 ...
随机推荐
- 错误号码2003 Can't connect to MySQL server 'localhost' (0)
错误描述 错误原因 最近,我一直都可以用SQLyog连接本地数据库,但是近几天却无法连接:并且一直都报上述错误,我查阅了很多资料,发现有很多中说法 总结一下 第一,MySQL中的my.ini出错: 第 ...
- 从html页面加载顺序来更好的理解jquery初始化
一,html页面加载顺序 1,用户输入网址(假设是个html页面,并且是第一次访问),浏览器向服务器发出请求,服务器返回html文件:2,浏览器开始载入html代码,发现<head>标签内 ...
- ThreadLocal原理
ThreadLocal类可以看作是当前线程的一个局部变量,只有当前线程可以访问,因此是线程安全的. ThreadLocal内部维护了一个ThreadLocalMap类,ThreadLocalMap是一 ...
- 用Python来找合适的妹子
时间真的有点仓促,匆匆忙忙撸完这篇文章. 虽然今天是情人节,但还是要关心一下单身狗们,帮助他们俩脱单. 古人云:知己知彼,百战不殆. 好好去了解一下妹子们的内心想法,早日脱单! 这次我在一个某知名婚 ...
- sass学习笔记--摘录
//$a: Helvetica, sans-serif //$b: #333 // //body //font: 100% $a //color: $b //$a: red //body //colo ...
- 【Luogu3806】点分治(点分治)
[Luogu3806]点分治(点分治) 题面 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入格式: n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接下来m行 ...
- [LOJ2230][BJOI2014]大融合
题面戳我 sol LCT维护子树size. 开一个数组\(sz_i\)表示一个节点的所有虚儿子的size和,\(sum_i\)表示以一个节点为根的子树的\(size\)和,可见\(sz_u=\sum_ ...
- [BZOJ1609] [Usaco2008 Feb] Eating Together麻烦的聚餐 (dp)
Description 为了避免餐厅过分拥挤,FJ要求奶牛们分3批就餐.每天晚饭前,奶牛们都会在餐厅前排队入内,按FJ的设想所有第3批就餐的奶牛排在队尾,队伍的前端由设定为第1批就餐的奶牛占据,中间的 ...
- webpack中hash与chunkhash区别和需要注意的问题
项目发布时,为了解决缓存,需要进行md5签名,这时候就需要用到 hash 和 chunkhash等. 问题一:hash问题 使用 hash 对js和css进行签名时,每一次hash值都不一样,导致无法 ...
- mysql url 连接配置的一个小坑。 工作中不会遇到。 学习的时候会
<property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> & ...