A 1096 Consecutive Factors
Problem Description
Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3×5×6×7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you are supposed to find the maximum number of consecutive factors, and list the smallest sequence of the consecutive factors.
Input
Each input file contains one test case, which gives the integer N (1<N<231 ).
Output
For each test case, print in the first line the maximum number of consecutive factors. Then in the second line, print the smallest sequence of the consecutive factors in the format factor[1]*factor[2]*...*factor[k]
, where the factors are listed in increasing order, and 1 is NOT included.
Sample Input:
630
Sample Output:
3
5*6*7
题目大意:
给你一个数n,求一个最长自然数乘积序列,这个序列的乘积是n的因数。
解题思路:
用dp数组记录x的阶乘,因为12的阶乘小于int,13的阶乘大于int,所以数组开到13就够了。先找是否有长度大于1的序列,如果没找到,就找n的最大因数然后输出。
AC代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[13],n,res=0,l,r;
void getN(){
int cnt=1,ans=1;
while(cnt<13){
ans*=cnt;
dp[cnt++]=ans;
}
return ;
}
int main(){
getN();
scanf("%d",&n);
for(int i=2;i<13;i++){
for(int j=12;j>i && j-i+1>res;j--){
int now=dp[j]/dp[i-1];
if(now>n) continue;
if(n%now==0){
res=j-i+1;
l=i;
r=j;
}
}
}
if(res==0){
int limit=sqrt(1.0*n);
for(res=2;res<=limit;res++){
if(n%res==0) break;
}
if(res==limit+1) res=n;
printf("1\n%d",res);
return 0;
}
printf("%d\n",res);
for(int i=l;i<=r;i++){
if(i>l) printf("*");
printf("%d",i);
}
return 0;
}