UPD:将写错的 改为 ,并解决了代码在输入 时可能会 WA 的问题。
我竟然差点不会 CSP-J2 T1 了……以下是考场做法。
枚举(其实不难证明)可知, 轮过后, 个苹果被拿走 个; 轮过后,剩下的苹果以 为周期,且剩下大约 个苹果。例如:
- 轮过后,所有编号模 余 的苹果会被拿走;
- 轮过后,所有编号模 余 的苹果会被拿走;
- ……
于是,我们先暴力模拟 个苹果拿 轮后的结果,算出周期,接着选出剩下大约 个苹果继续暴力模拟即可。
当 时最优,显然此时 ,时间 / 空间复杂度为 。
下取 即可。代码非常「好写」,仅仅 行;性能也非常「优秀」,仅仅 67 ms / 19.79 MB。
AC Code:
#include<bits/stdc++.h>
using namespace std;
queue<int> q,qq;
int main()
{
int n,ans1=0,ans2=0,u,c;
cin>>n;
int mod=1;
for(int i=1;i<=13;i++)
mod*=3;
if(n<=mod)
{
for(int i=1;i<=n;i++)
q.push(i);
int rounds=0;
while(!q.empty())
{
rounds++;
c=q.size();
for(int i=1;i<=c;i++)
{
u=q.front();
q.pop();
if(i%3!=1) q.push(u);
else if(u==n) ans2=rounds;
}
}
ans1=rounds;
}
else
{
for(int i=1;i<=mod;i++)
q.push(i);
for(int rd=1;rd<=13;rd++)
{
c=q.size();
for(int i=1;i<=c;i++)
{
u=q.front();
q.pop();
if(i%3!=1) q.push(u);
else if(n%mod==u%mod) ans2=rd;
}
}
int sz=q.size();
for(int st=0;st<=n;st+=mod)
for(int j=1;j<=sz;j++)
{
u=q.front();
q.pop();
q.push(u);
if(st+u>n) break;
qq.push(st+u);
}
int rounds=13;
while(!qq.empty())
{
rounds++;
c=qq.size();
for(int i=1;i<=c;i++)
{
u=qq.front();
qq.pop();
if(i%3!=1) qq.push(u);
else if(u==n) ans2=rounds;
}
}
ans1=rounds;
}
cout<<ans1<<' '<<ans2;
return 0;
}