看上去漏洞百出,实际上还是漏洞百出求调
查看原帖
看上去漏洞百出,实际上还是漏洞百出求调
1294258
zhangyuexuan001楼主2024/11/9 19:33
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,a[100005],num=0,c[100005];
long long Pow(long long a,long long b){
	if(b==0){
		return b;
	}
	if(b%2==0){
		long long res=Pow(a,b/2);
		res=res*res;
		return res;
	}
	return a*Pow(a,b-1);
}
int shai(int n){
	for(int i=2;i*i<=n;i++){
		if(!a[i]){
			for(int j=i*i;j<=n;j+=i){
				a[j]=true;
			}
		}
	}
	for(int i=n;i>=2;i++){
		if(!a[i]){
			return i;
		}
	}
}
int main(){
	cin>>n;
	while(n--){
		cin>>m;
		int cnt=0;
		bool temp=0;
		while(m>0){
			num++;
			if((Pow(2,num-1)>shai(m))){
				cnt++;
				m-=Pow(2,num-1);
			}else{
				if(temp==false){
					cnt++;	
					temp=true;
					m-=shai(m);
				}else{
					cnt++;
					m-=Pow(2,num-1);
				}
				
			}
		}
		if(m==0){
			cout<<cnt;
		}else{
			cout<<-1; 
		}
	}
	return 0;
}
2024/11/9 19:33
加载中...