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,问你有多 ...
随机推荐
- Codeforces Round #464 (Div. 2) E. Maximize!
题目链接:http://codeforces.com/contest/939/problem/E E. Maximize! time limit per test3 seconds memory li ...
- BFS:CF356C-Compartments
C. Compartments time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- day19 Dgango进阶 路由系统及 ORM 详解
完成一个完整的网页服务,需要有以下: 再次回顾一下Django 的完成开发流程: 一些值的获取: 对于性别,为互斥属性: 爱好则为多选: 需要使用新的方法 getlist 来获取多个爱好: 单选下拉框 ...
- hdu3374 String Problem 最小最大表示法 最小循环节出现次数
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- JS的跨域理解
前言 周一的学院点开题被批的很惨,换了个校长,各种被抓严,班上已经有两个同学打算休学了.哎,这周的聚会可能是大家集聚的最后一次吧.熬着吧,还是学习我的前端,不管老板学校咋逼了,找个好工作才是王道.今天 ...
- 【Word Break】cpp
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- IOS开发学习笔记026-UITableView的使用
UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...
- Aptana Studion出现 duplicate location重复定位报错
1.下载SVN的时候出现报错 duplicate location 2.点击“available software sites”查看已可用的软件网站 3.在这里可以查看到SVN,勾选SVN复选框,点击 ...
- git和github基础入门
一.git: 1.安装配置git: 1.1从官网或者该网址处下载:https://pan.baidu.com/s/1kU5OCOB#list/path=%2Fpub%2Fgit 1.2安装,一路nex ...
- python 字符编码与转码
一. 字符编码 ASCII: 一个字节,最多能表示255个字符 GB2312(1980年):一共收录了7445个字符,包括6763个汉字和682个其它符号. GBK1.0(1995年):收录了2188 ...