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


    题意(Codeforces 940B)

    对一个数字$x$,你有两个决策:花费$A$减一、或花费$B$除以$k$(但必须可以除尽)。问使之到$1$的最少花费。

    分析

    贼鸡儿简单,但我花式犯蠢……如果除不尽,那么直接用法一减到可以除得尽的;然后比较法一和法二哪个耗费得少,然后继续如上操作直到$x=1$。典型的贪心算法。
    是不是看起来很美?做梦啊!!对于法一,当$x<k$,不能减到除得尽(0)!对于比较耗费,如果$k=1$,只能选择法一,哪怕法二再怎么耗费少都不能啊啊啊啊……
    这两个坑踩过去了题目一点难度没有。

    源码

    #include<bits/stdc++.h>
    
    #define inf 0x3f3f3f3f
    #define PB push_back
    #define MP make_pair
    #define fi first
    #define se second
    #define lowbit(x) (x&(-x))
    #define rep(i, a, b) for(int i = (a); i <= (b); i++)
    #define per(i, a, b) for(int i = (a); i >= (b); i--)
    #define pr(x) cout << #x << " = " << x << " ";
    #define prl(x) cout << #x << " = " << x << endl;
    #define ZERO(X) memset((X),0,sizeof(X))
    #define ALL(X) X.begin(),X.end()
    #define SZ(x) (int)x.size()
    
    using namespace std;
    
    typedef pair<int,int> PI;
    typedef pair<pair<int,int>, int> PII;
    typedef pair<pair<pair<int,int>, int>, int> PIII; 
    typedef unsigned long long ull;
    typedef long long ll;
    typedef long double lb;
    #define quickio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
    #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
    /*      debug("Precalc: %.3f
    ", (double)(clock()) / CLOCKS_PER_SEC);
    clock_t z = clock();
            solve();
            //debug("Test: %.3f
    ", (double)(clock() - z) / CLOCKS_PER_SEC);
    */
    template<typename T = int>
    inline T read() {
        T val=0, sign=1;
        char ch;
        for (ch=getchar();ch<'0'||ch>'9';ch=getchar())
            if (ch=='-') sign=-1;
        for (;ch>='0'&&ch<='9';ch=getchar())
            val=val*10+ch-'0';
        return sign*val;
    }
    int main()
    {
        ll n,k,A,B;
        cin>>n>>k>>A>>B;
        ll x=n;
        ll ans=0;
        if(k==1)
        {
            ans=A*(x-1); //无能狂怒啊
        }
        else
        {
            while(x!=1)
            {
                if(x%k)
                {
                    if(x<k)
                    {
                        ans+=(x-1)*A;
                        x=1;
                    }
                    else
                    {
                        ans+=(x%k)*A;
                        x=x-(x%k);
                    }
    
                }
                else
                {
                    ans+=min((x/k*(k-1))*A,B);
                    x/=k;   
                }
                //cout<<x<<endl;
                //assert(x!=0);
            }
        }
    
        cout<<ans<<endl;
        return 0;
    }
    如非注明,原创内容遵循GFDLv1.3发布;其中的代码遵循GPLv3发布。
  • 相关阅读:
    2016中国大学生程序设计竞赛
    HDU 1671 Phone List (Trie·数组实现)
    Codeforces Round #367 (Div. 2) Hard problem
    UVA 133 The Dole Queue
    SG函数模板
    Codeforces Round #366 (Div. 2) C Thor(模拟+2种stl)
    [Offer收割]编程练习赛4 A 满减优惠
    CF #365 (Div. 2) D
    Codeforces Round #365 (Div. 2) Chris and Road
    codeblocks AStyle修改格式和快捷键
  • 原文地址:https://www.cnblogs.com/samhx/p/cfr466d2b.html
Copyright © 2020-2023  润新知