一个正整数 N 的因子中可能存在若干连续的数字。例如 630 可以分解为 3×5×6×7,其中 5、6、7 就是 3 个连续的数字。给定任一正整数 N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。
输入格式:
输入在一行中给出一个正整数 N(1<N<231)。
输出格式:
首先在第 1 行输出最长连续因子的个数;然后在第 2 行中按
因子1*因子2*……*因子k
的格式输出最小的连续因子序列,其中因子按递增顺序输出,1 不算在内。输入样例:
630
输出样例:
3 5*6*7
暴力枚举
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int,int>PII;
constexpr int N=1e5+7;
int prime[N];
vector<int>vi;
signed main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
int sq= sqrt(n);
for(int len=12;len>=1;len--){
for(int start=2;start<=sq;start++){
int ans=1;
for(int i=start;i-start+1<=len;i++){
ans*=i;
}
if(n%ans==0){
cout<<len<<endl<<start;
for(int i = start + 1; i - start <= len - 1; i++){
cout<<"*"<<i;
}
return 0;
}
}
}
cout<<1<<endl<<n<<endl;
return 0;
}