sb题,你可以
1.暴力n−−√枚举因子
2.枚举答案,判是否可行
hint:注意负数
#include <stdio.h>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long LL;
LL solve(LL n){
int flag=0;
if(n<0){n=-n,flag=1;}
LL mx=sqrt(n);
for(LL i=2;i<=mx;i++){
LL ts=n,cnt=0;
while(ts%i==0){
ts/=i;
cnt++;
}
if(ts==1){
if(!flag)return cnt;
else if(cnt&1) return cnt;
}
}
return 1;
}
int main(void)
{
int t,cas=0;
scanf("%d",&t);
while(t--){
LL n;
scanf("%lld",&n);
printf("Case %d: %lld\n",++cas,solve(n));
}
return 0;
}

本文介绍了一种通过枚举因子解决特定数学问题的算法。该算法可以处理正负整数输入,并采用暴力枚举的方式找到所有因子。核心部分包括简化运算以提高效率的方法,如仅枚举到平方根范围内的因子等。文章提供了完整的C++实现代码示例。
234

被折叠的 条评论
为什么被折叠?



