跳到正文

题解

题解 P9748 [CSP-J 2023] 小苹果

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
获赞
47
题目
P9748

版本

  1. v1当前

撞上我曾经出给 CROI R1 的原题了,很强。


思路

假设这一天还剩 xx 个苹果。 我们每 33 个苹果分一组,剩余分不满一组的也分一组。显然从每组中会拿走 11 个 苹果。 所以这一天将拿走 ⌈x3⌉\left\lceil\dfrac{x}{3}\right\rceil 个苹果。

每次拿走的是一组中的第 11 个苹果,而一组一共有 33 个苹果。 所以当且仅当 imod⁡3=1i \operatorname{mod} 3=1 时,第 ii 个苹果会被拿走。 那么我们每天重新算一下 nn 前面有几个苹果,我们就可以知道 nn 在这一天排在第几个。当某天 nmod⁡3=1n \operatorname{mod} 3=1 时,将在这一天被拿走。


代码

C++
#include<bits/stdc++.h>
using namespace std;
int n,x;
int ans,cnt;
int main(){

	scanf("%d",&n);
	x=n;

	while(x){//还剩了苹果就一直取
		cnt++;
		x-=(x+2)/3;//这个相当于想上取整
	}
	printf("%d ",cnt);

	while(1){
		ans++;
		if(n%3==1)break;//取走了
		n-=(n+2)/3;//下一天剩多少苹果
	}
	printf("%d",ans);
	return 0;
}
原文