主要思路:
因为本题是要输出连续的因子,所以只用记录开始的数和长度。
注意:
判断一个数的因子的时候,上界是sqrt(n),但是这个值可以取到,即必须是小于等于,不然会出错。
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long LL;
int main(){
LL n;
scanf("%lld",&n);
LL st,maxL=0;
LL sqr = (LL)sqrt(1.0*n);
for(LL i=2;i <= sqr; i++){
LL temp = 1,j = i;
while(1){
temp =temp * j;
if(n % temp != 0) break;
if(j - i + 1 > maxL){
maxL = j-i + 1;
st = i;
}
j++;
}
}
if(maxL == 0) printf("1\n%lld",n);
else{
printf("%lld\n",maxL);
for(LL i = st;i< st+maxL; i++){
printf(i==st?"%lld":"*%lld",i);
}
}
return 0;
}

本文介绍了一种算法,用于寻找给定整数的最大连续因子序列。通过遍历从2到sqrt(n)的所有可能因子,算法记录下最长的连续因子序列及其起始值,最后输出序列的长度和具体因子。关键在于利用数学性质减少不必要的计算。
232

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



