题解:P14027 【MX-X20-T1】「FAOI-R7」加训加训

· · 题解

题目大意

题目传送门

题目分析

显然,当 a 的初值为 0 或 1 时,普通训练一定比加训加训好。因为当 a = 0 时,怎么乘都是 0。当 a = 1 时,如果加训,则 a 变为 k,如果普通训练,则 a 变为 k + 1,显然后者更优。
剩下的直接乘就完了。

::::warning[注意] 一定要特判 k = 1 的情况,不然会死循环 TLE。 ::::

代码实现

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'

#define int long long

#define fst ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int a,b,k;// 如题意所述
int ans=0;

signed main()
{
    fst
    cin>>a>>b>>k;
    if(k==1){// 一定要特判
        cout<<b-a;
        return 0;
    }
    while(a<b){
        if(a==0||a==1){
            ans++;
            a+=k;
            continue;
        }
        ans++;
        a*=k;
    }
    cout<<ans;
    return 0;
}