跳到正文

题解

题解:P17496 [ICPC 2026 Wuhan I] Prime Game

LXcjh4998约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年9月27日
收录
2026年9月29日
更新
2026年11月10日
篇幅
约 6 分钟
获赞
3
题目
P17496

版本

  1. v1当前

进行一个回合后,nn 一定减小了。
这是因为 nn 至多变成了 2n−12\dfrac{2n-1}{2} 状物。
所以答案随着 nn 增加不减。
所以你每次只要最大化一个回合后的 nn 即可,我们称这个为 nxt⁡(n)\operatorname{nxt}(n)。

考虑后手选择的质数。
显然他会选最大的质因数。
你发现若选择 maxp=5maxp=5,则最多到 2n5\dfrac{2n}{5},比 n2\dfrac{n}{2} 小。
但是 maxp=3maxp=3 是可能优于 maxp=2maxp=2 的。
所以 maxp=2,3maxp=2,3。

maxp=2maxp=2 是容易的。
maxp=3maxp=3 就是枚举下 33 的个数,然后选择个范围内最大的 22 的幂乘上去即可。
所以求一次 nxt⁡\operatorname{nxt} 的复杂度是 O(log⁡n)O(\log n)。

感性理解下答案应该是 O(log⁡n)O(\log n) 的,我们来证一下。
若选择 maxp=3maxp=3,则 nn 至多变成 2n3\dfrac{2n}{3}。
若选择 maxp=2maxp=2,则此后 nn 变为 22 的幂,下一次必定选择 maxp=3maxp=3。
故每 22 个回合 nn 就必定至多为 2n3\dfrac{2n}{3}。

时间复杂度 O(log⁡2n)O(\log^2 n)。

code
C++
#include<bits/stdc++.h>
//#include<atcoder/all>
#define forUp(i,a,b) for(int i=(a);i<=(b);++i)
#define forUP(i,a,b) for(int i=(a);i<(b);++i)
#define forDown(i,a,b) for(int i=(a);i>=(b);--i)
#define forG(u,v) for(int __i=head[u],v=to[__i];__i;__i=nxt[__i],v=to[__i])
#define forWG(u,v,c) for(int __i=head[u],v=to[__i],c=w[__i];__i;__i=nxt[__i],v=to[__i],c=w[__i])
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define popc __builtin_popcount
#define popc64 __builtin_popcountll
#define seteps(n) fixed<<setprecision(n)
bool __mst;using int16=short;using uint16=unsigned short;using uint=unsigned int;using int64=long long;using uint64=unsigned long long;using int128=__int128;using uint128=unsigned __int128;
using float64=double;using float80=long double;constexpr int INF=0x3f3f3f3f,MINF=0xcfcfcfcf;constexpr int64 INF64=0x3f3f3f3f3f3f3f3f,MINF64=0xcfcfcfcfcfcfcfcf;constexpr float64 INFDB=1e50,eps=1e-6;
template<class _Tp>inline void chkMax(_Tp &x,const _Tp &y){x<y?x=y:x;}template<class _Tp>inline void chkMin(_Tp &x,const _Tp &y){x>y?x=y:x;}
inline int addMod(const int &x,const int &y,const int mod){int ans=x+y;return ans>=mod?ans-mod:ans;}inline int subMod(const int &x,const int &y,const int mod){int ans=x-y;return ans<0?ans+mod:ans;}inline int mulMod(const int &x,const int &y,const int mod){return 1ll*x*y%mod;}
constexpr int N=1e6+10;int __test_num=1,__task_id,__tst;using namespace std;void __init();

int64 get(int64 x){
    int64 ans2=1ll<<__lg(x-1);
    int64 ans3=0,mul=3;while(mul<(x<<1)){
        int64 L=x/mul,R=(2*x-1)/mul;
        int64 y=1ll<<__lg(R);if(L<=y)chkMax(ans3,y*mul);
        mul*=3;
    }
    ans3/=3;
    return max(ans2,ans3);
}
int64 calc(int64 n){
    int ans=0;
    while(n>1)n=get(n),++ans;
    return ans;
}

bool __med;void __solve(int __test_id){
    int64 n;cin>>n;
    cout<<calc(n)<<'\n';
}
signed main(){
    __init();
    forUp(i,1,__test_num)__solve(i);
    cerr<<1000.0*(clock()-__tst)/CLOCKS_PER_SEC<<"ms "<<((&__mst)-(&__med))/1024.0/1024.0<<"MB"<<'\n';
    return 0;
}
void __init(){
    __tst=clock();
    #ifndef use_file
    //#define use_file
    #endif
    #ifdef use_file
    const string __file_name="test";freopen((__file_name+".in").c_str(),"r",stdin);freopen((__file_name+".out").c_str(),"w",stdout);
    #endif
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //scanf("%d",&__test_num);
    //cin>>__task_id;
    //cin>>__test_num;
}
原文
存档评论4 条抓取于