Codeforces Round #466 (Div. 2) B. Our Tanya is Crying Out Loud[将n变为1,有两种方式,求最小花费/贪心]
1 second
256 megabytes
standard input
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:
- 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?
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 a single integer — the minimum amount of coins you have to pay to make x equal to 1.
9
2
3
1
6
5
5
2
20
8
19
3
4
2
12
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,有两种方式① n-1花费a , ② 如果n能够被k整除,n=n/k,花费b.求最小花费.
[分析]:很显然这是道贪心题,
一个很容易错的地方就是想着优先除二,然后在减一,
因为没有考虑两者的花费,如果b很大的话就错了,所以每次都要判断下,然后注意细节就行了
首先如果k==1,那么ans=(n-1)*a;
n%k!=0,只能执行操作1
n<k,只能执行操作1
n%k==0(n>=k) 的情况下比较n到达相同结果时两种操作的花费即可。
[代码]:
/*
题意:将n变为1,有两种方式①n-1花费a,②如果n能够被k整除,n=n/k,花费b,求最小花费
*/
/*
很显然这是道贪心题,
一个很容易错的地方就是想着优先除二,然后在减一,
因为没有考虑两者的花费,如果b很大的话就错了,所以每次都要判断下,然后注意细节就行了
*/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mem(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=;
const int inf=0x3f3f3f3f;
int main()
{
ll x,n,k,a,b,ans;
while(~scanf("%lld%lld%lld%lld",&n,&k,&a,&b)){
ans=;
if(k==) return *printf("%lld\n",a*(n-)); //注意特判k==1的情况,防止死循环
while(n!=){
//n无法整除k
if(n%k!=){ //不能整除只能减法(a),减到整除为止
ans+=a*(n%k);
n-=(n%k);
}
if(n<k){ //此时只能减法(a),除法永远用不到,直接求解答案
ans+=a*(n-);
break;
}
//n可以整除k
ans+=min(b,a*(n-n/k)); //减法(a)和除法(b)取最小花费
n/=k; //n可以整除k
}
printf("%lld\n",ans);
}
}
/*
n-n/k*k = n%k
获取距离n最近的k的倍数的方法是:n/k*k = n-n%k
*/
贪心
Codeforces Round #466 (Div. 2) B. Our Tanya is Crying Out Loud[将n变为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 ...
- 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 Round #466 (Div. 2) -A. Points on the line
2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 s ...
- Codeforces Round #466 (Div. 2) A. Points on the line[数轴上有n个点,问最少去掉多少个点才能使剩下的点的最大距离为不超过k。]
A. Points on the line time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #324 (Div. 2) Kolya and Tanya 组合数学
原题链接:http://codeforces.com/contest/584/problem/B 题意: 有3*n个人围成一个圈,每个人可以分配1到3个硬币,但是相邻为n的三个人的和不能是6,问你有多 ...
随机推荐
- SHIWEITI
//Wannafly挑战赛19(牛客网) //A 队列Q #include <iostream> #include <cstdio> #include <cstring& ...
- Ubuntu下Python无法识别中文
在NLP的相关任务中,应用python处理中文是很常见的.在这个过程中,由于编码方式的不一致,可能会出现以下两种错误: 1)SyntaxError: Non-ASCII character in f ...
- Unity脚本执行顺序自研框架
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52372611 作者:car ...
- datagrid的修改和删除功能的实现
1.修改 双击,进入一行的编辑状态的功能的实现 2.删除 3.扩展easyui的datagrid,添加动态增加或删除Editor的方法 (1)背景要求: 对于某一列,比如密码,动态增加时候,是可以编辑 ...
- MySQL基础3-SQL语言
1.DQL语句分类 重点在于Select语句 2.Sql语句的书写规则 3.怎样使用Navicat导入已经写好的sql文件 (1)在Navicat中右击选中的数据库 (2)将sql文件所在的路径添加进 ...
- Three Steps to Migrate Group Policy Between Active Directory Domains or Forests Using PowerShell
Three Steps Ahead Have you ever wished that you had three legs? Imagine how much faster you could ru ...
- PS教程超级合辑【800+集爆款课】
第1章 导读——推荐大家到网易云课堂学习购买(本博文仅为个人学习笔记)https://study.163.com/course/courseMain.htm?courseId=1442008& ...
- day02_01.能被3整除的数
第1题 能被3整除的数 编程思想的初步形成 把人的正常思维放大化,用放大镜去放大你的每个思考过程 你会发现,原来编程没有你想象的那么难 题目:输出100以内(不含100)能被3整除的所有整数 < ...
- [oldboy-django][2深入django]后台生成form标签并设置标签的属性
# Form生成html标签 a. 通过Form生成Input输入框,Form标签,以及submit标签还是要在前端写的, 但是Form标签内的Input标签可以在后台实现:只需要按以下步骤 - vi ...
- 平时代码中不符合python风格的举例
良好的代码风格体现出自己的专业,良好的代码风格,方便同事之间协作. 下面举例讲讲自己在代码中不符合python风格的一些情况,方便自己写出优雅的代码. 段落之间空两行 很长的参数用括号连接但是不要超过 ...