跳到正文

题解

P9748题解

Vitamin_B约 1 分钟
原文
文章信息与版本

文章信息

发布
2023年10月24日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 1 分钟
题目
P9748

版本

  1. v1当前

题目传送门

思路

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

代码

C++
# 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;
}
原文
存档评论2 条抓取于