码迷,mamicode.com
首页 > 其他好文 > 详细

codeforce round#466(div.2)B

时间:2018-02-24 21:54:58      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:AC   cst   tor   inpu   tac   freopen   copy   form   out   

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

详细见代码

技术分享图片
 1 #define debug
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<cmath>
 5 #include<queue>
 6 #include<stack>
 7 #include<string>
 8 #include<cstring>
 9 #include<string.h>
10 #include<algorithm>
11 #include<iostream>
12 #include<vector>
13 #include<functional>
14 #include<iomanip>
15 #include<map>
16 #include<set>
17 #define pb push_back
18 using namespace std;
19 typedef long long ll;
20 pair<ll,ll>PLL;
21 pair<int,ll>Pil;
22 const int INF = 0x3f3f3f3f;
23 const double inf=1e8+100;
24 const ll maxn =1000;
25 const int N = 1e4+10;
26 const ll mod=1000007;
27 ll n,d;
28 ll A,B,k;
29 
30 void solve() {
31     int i,j,t=1;
32 //    cin>>t;
33     while(t--) {
34         ll ans=0;
35         cin>>n>>k>>A>>B;
36         if(k==1)
37             ans=(n-1)*A;
38         else {
39             while(n!=1) {
40                 if(k<=n) {
41                     if(n%k) {
42                         ans+=(n%k)*A;
43                         n-=n%k;
44                     } else {
45                         ans+=min(B,(n-n/k)*A);
46                         n/=k;
47                     }
48                 }
49                 else{
50                     ans+=(n-1)*A;
51                     n=1;
52                 }
53             //    cout<<ans<<endl;
54             }
55         }
56         cout<<ans<<endl;
57     }
58 }
59 
60 
61 int main() {
62     ios_base::sync_with_stdio(false);
63 #ifdef debug
64     freopen("in.txt", "r", stdin);
65 //    freopen("out.txt","w",stdout);
66 #endif
67     cin.tie(0);
68     cout.tie(0);
69     solve();
70     return 0;
71 }
View Code

 本人也是菜鸡,如有写的不好的地方请指出,谢谢

codeforce round#466(div.2)B

标签:AC   cst   tor   inpu   tac   freopen   copy   form   out   

原文地址:https://www.cnblogs.com/visualVK/p/8467577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!