easy math problem(2)

easy math problem

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

对于一个数n,有以下两种操作:

一是减一,需要花费 a 。

但是如果 n 能被 k 整除,还可以花费 b 让 n 除以 k。

请问将这个数变为1最少要多少花费?

Input

第一行一个整数n(n<=1e5)

第二行三个正整数分别为a, b, k ( 0 < a , b , k <= n ,n*a<1e9 ).

Output

输出一个整数代表最小花费。

Sample Input

10
1 2 2

Sample Output

6

Hint

Source

行走的二叉树

题目提交链接

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,sum=0;
    scanf("%d",&n);
    int k,a,b;
    scanf("%d%d%d",&a,&b,&k);
    while(n!=1)
    {
        if((n/k==1)&(n%k==0))//注意:5/3=1
        {
            if(a*n>b)
                sum= sum +b;
            else
                sum= sum +a;
            break;
        }
        else
        {
            if(n%k==0)
            {

                sum +=b;
                n = n/k;
            }
            else
            {
                sum +=a;
                n = n-1;
            }
        }
    }
    printf("%d\n",sum);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值