P9748题解

· · 题解

题目传送门

思路

这题我们可以模拟取苹果的过程。每天取 \lceil\frac n3\rceil 个苹果,直到取完,时间复杂度是 \log_3n,足以通过。第二问,我们只要看哪时候第一次取到最后一个苹果即可,取到最后一个苹果要满足的条件就是 n\bmod3=1。然后我们就可以愉快的通过此题啦!

代码

# include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int n, ans1, ans2;
int main () {
    ios::sync_with_stdio (0);
    cin.tie (0);
    cout.tie (0);
    cin >> n;
    while (n) { //现在还有苹果
        ++ ans1; //天数+1
        if (n % 3 == 1 && ! ans2) //能取到最后一个苹果,且是第一次
            ans2 = ans1;
        n -= (n + 2) / 3; //别忘了减去拿走的苹果!
    }
    cout << ans1 << ' ' << ans2;
    return 0;
}