传送门

题目大意

给你n,k,A,B四个数,x=n,有两种操作:

1.将x-1,需支付A个金币

2.将x÷k,需支付B个金币,当且仅当k能整除x时可进行此操作

问将x修改为1至少要花几个金币

分析

模拟两个过程,如果k能整除x则判断一个一个将x减到想x/k和将x除以k哪个花费的金币少,否则如果x大于k将x减去x模k的余数(不可每次减1,减若干次,否则会超时),x小于k则减去(x-1)。注意特判k等于1的情况。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int main()
{     long long n,m,i,j,k,x,a,b,ans=0;
      cin>>n>>k>>a>>b;
      x=n;
      if(k==1){
          cout<<(n-1)*a<<endl;
          return 0;
      }
      while(x!=1){
          if(x%k!=0){
              if(x>k){
                ans+=(x%k)*a;
                x=x-x%k;
              }else {
              ans+=(x-1)*a;
              x=1;
            }
          }else {
              if((x-x/k)*a<b){
                  ans+=(x-x/k)*a;
                  x=x/k;
              }else {
                  x=x/k;
                  ans+=b;
              }
          }
      }
      cout<<ans<<endl;
      return 0;
}

940B Our Tanya is Crying Out Loud的更多相关文章

  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 ...

  2. 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 ...

  3. 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 ...

  4. 「日常训练」Our Tanya is Crying Out Loud (CFR466D2B)

    题意(Codeforces 940B) 对一个数字$x$,你有两个决策:花费$A$减一.或花费$B$除以$k$(但必须可以除尽).问使之到$1$的最少花费. 分析 贼鸡儿简单,但我花式犯蠢……如果除不 ...

  5. B. Our Tanya is Crying Out Loud

    http://codeforces.com/problemset/problem/940/B Right now she actually isn't. But she will be, if you ...

  6. Codeforces Round #466 (Div. 2) Solution

    从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...

  7. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  8. Codeforces Round #466 (Div. 2)

    所有的题目都可以在CodeForces上查看 中间看起来有很多场比赛我没有写了 其实是因为有题目没改完 因为我不想改,所以就没有写了(大部分题目还是改完了的) 我还是觉得如果是打了的比赛就一场一场写比 ...

  9. Codeforces Round #466 (Div. 2) 题解

    人生中第三次\(CF\)... 考试中切了\(A\)~\(E\) \(F\)题会做没时间写 题解 A:Points on the line 题意 给定一个数列,删最小的数,使最大差不大于一个定值 So ...

随机推荐

  1. Linux apache的运行用户和用户组

    我们在安装apache后,有时在上传文件的时候,提示没有权限或者是不可写,我们都会去查文件夹的权限. 通过ls -l /var/www/html/website可以很直观的看出我们文件和文件夹的权限, ...

  2. libz.dylib

    1. .dylib意味着这是一个动态链接库. 2. libz.dylib是提供zip压缩解压缩的库

  3. Algorithms code

    一些值得回看的小算法. 最长的连续子数组 子数组数字不重复 int [] arr={1,2,3,4,7}; //输出4 int [] arr1={1,2,3,4,1,2,3,4,5,1}; //输出5 ...

  4. 2017-06-23(chmod whoami chown)

    文件权限设定 chmod u-x newfile chmod u-x,g+w newfile chmod a=rwx newfile [mode=421] r = 4 , w=2, x=1 chmod ...

  5. linux_vi快捷键

    vi有哪些快捷方式? 到行头: 0 ^ home 到行尾: $ shif+a(编辑模式) end 退出保存: wq . x .wq!(强制退出保存) 强制退出不保存: q! 光标移到文件最后一行: s ...

  6. Maven的Archetype简介

    Archetype,骨架的意思. 文章出处:http://m.blog.csdn.net/blog/FireOfStar/42526027 Archetype是什么? 简单的说,Archetype是M ...

  7. common-logging--源码之SimpleLog

    common-logging源码Log接口 在common-logging的源码中,将log核心类抽象成了一个Log接口. 这里贴出Log接口的源码: /* * Licensed to the Apa ...

  8. maven的聚合和继承

    Maven的聚合特性能够把项目的各个模块聚合在一起构建: 而Maven的继承特性则能帮组抽取各模块相同的依赖和插件等配置,在简化POM的同时,还能促进各个模块配置的一致性. 聚合:新建一个项目demo ...

  9. 【转】C++易混知识点1: 指针常量和常量指针的区别,附有详细案例解释

    熟悉C++也已经有一些年头了,今天突然翻出当年浏览的书籍,对一些概念居然生疏了,指针常量和常量指针由于 指针 这一特殊的对象而变得难以区别.因此,在思考再三之后,决定写下该篇总结,加强对他们的区别: ...

  10. 零基础实现node+express个性化聊天室

    本篇文章使用node+express+jquery写一个个性化聊天室,一起来get一下~(源码地址见文章末尾) 效果图 项目结构 实现功能 登录检测 系统自动提示用户状态(进入/离开) 显示在线用户 ...