[题解]P9748 [CSP-J 2023] 小苹果
思路
首先可以发现每一次会减少的数量都是可以计算出来的,为 \(1 + \lfloor \frac{n - 1}{3} \rfloor\)。
那么直接暴力枚举,当 \(n\) 变为 \(0\) 时结束,不难发现复杂度是 \(\Theta(n^{\frac{2}{3}})\) 级别的。
考虑第二问的答案,显然是第一次能将最后一个元素取到的时候,即第一次满足 \(n \bmod 3 = 1\) 的时候。
code
#include <bits/stdc++.h>
#define re register
#define int long long
using namespace std;
int n,id,num,ans;
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 1) + (r << 3) + (c ^ 48);
c = getchar();
}
return r * w;
}
signed main(){
n = read();
while (n){
num++;
if (n % 3 == 1 && !ans) ans = num;
n -= (1 + (n - 1) / 3);
}
printf("%lld %lld",num,ans);
return 0;
}

浙公网安备 33010602011771号