【剑指紫金港】1096 Consecutive Factors 阶乘

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<2​31​​ ).

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

征服所有不服

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值