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,问你有多 ...
随机推荐
- Linux命令之---which简单介绍
命令简介 which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果.也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的 ...
- wget常用下载命令
wget wget是一个从网络上自动下载文件的自由工具,支持通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理.wget名称的由来是“World Wide Web ...
- loj2049 「HNOI2016」网络
好像复杂度来说不是正解--不加谜之优化(下叙)能被loj上的加强数据卡 #include <algorithm> #include <iostream> #include &l ...
- Python框架之Django学习笔记(十七)
Django框架之表单(续二) 今天的这篇博客将是Django学习笔记博客的最后一篇,基本每周最少一篇的Django框架学习,坚持到今天也实属不易,当然了,这个框架的学习仅仅是Django框架的基础部 ...
- Python基础-week06 面向对象编程基础
一.面向对象编程 1.面向过程 与 面向对象编程 面向过程的程序设计: 核心是 过程二字,过程指的是解决问题的步骤,即先干什么再干什么......面向过程的设计就好比精心设计好一条流水线,是一种机械式 ...
- python的重重之器(生成器、迭代器、装饰器)
一.装饰器 1.定义:本质是函数,装饰其他函数就是为其他函数添加附件功能. 2.原则: a.不能修改被装饰的函数的源代码: b.不能修改被装饰的函数的调用方式: 实例: #!/usr/bin/env ...
- redis设置最大内存上限对置换策略的解读
现在很少服务器还在使用32位的操作系统了,所以服务器的内存可以接近极限2^64的字节.redis配置文件中有限制最大内存的字段maxmemory,当redis的key达到最大值时,redis会有多种策 ...
- 模板与c++11--右值引用
函数参数传递 struct huge_data{ char *content; unsigned sz; huge_data():content(),sz(){ cout<<this< ...
- hdu 4176
Class Statistics Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- MySQL 添加审计功能
MySQL社区版没有自带的设计功能或插件.调研发现MariaDB的audit plugin 同样适用于MySQL,支持更细粒度的审计,比如只审计DDL操作,满足我们的需求.因为最近测试环境的某表结构经 ...