先给出我的代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long LL;
typedef unsigned int UI;
typedef unsigned long long ULL;
typedef __int128 big;
int T,n,ans[N],pos;
bool check[N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
for(int i=2;i<=1e5;i++)
{
if(!check[i]) ans[++pos]=i;
for(int j=1;j<=pos&&ans[j]*i<=1e5;j++)
{
check[ans[j]*i]=true;
if(i%ans[j]==0) break;
}
}
cin>>T;
while(T--)
{
cin>>n;
int ans=-1,cnt=0;
while(1)
{
if(!check[n])
{
ans=cnt+1;
break;
}
n-=(1<<cnt++);
if(n<=0) break;
}
if(ans==-1)
{
if(n<0) ans=-1;
else ans=--cnt;
}
cout<<ans<<'\n';
}
return 0;
}
这个代码中并没有把 1 设成非质数,因为我先弄了个 90,就是没有特判 1。
然后我觉得这个代码错在这:若 n 在减了至少 2 次的时候减到 1 了,那么应该答案就是 −1 了吧,但为什么我对了?求大佬解释。
回复请 at 我。