您的位置:首页 > 其它

L1-006. 连续因子

2017-05-08 10:45 393 查看

题目地址

https://www.patest.cn/contests/gplt/L1-006

题目描述

一个正整数N的因子中可能存在若干连续的数字。例如630可以分解为3*5*6*7,其中5、6、7就是3个连续的数字。给定任一正整数N,要求编写程序求出最长连续因子的个数,并输出最小的连续因子序列。

输入格式:

输入在一行中给出一个正整数N(1 < N < 2^31)。

输出格式:

首先在第1行输出最长连续因子的个数;然后在第2行中按“因子1*因子2*……*因子k”的格式输出最小的连续因子序列,其中因子按递增顺序输出,1不算在内。

输入样例:

630


输出样例:

3
5*6*7


ac

数据用long long类型,不用int

素数区别

连续因子

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <iostream>
#include <queue>

using namespace std;

const int N = 1005;

typedef long long int LL;

bool isPrime(LL n)
{
if(n == 2)
return true;

for(LL i= 2;i*i<=n;i++)
{
if(n % i == 0)
return false;
}
return true;
}

int main()
{
//freopen("in.txt", "r", stdin);
LL n;
while(scanf("%lld",&n) != EOF)
{
if(isPrime(n))
{
printf("1\n%lld\n",n);
continue;
}

LL ansCnt = 0;
LL staNum = 0;

LL sta = 2;
LL minSums = n;

while(sta * sta <= n)
{
if(n % sta == 0)
{
LL sums = sta;
int cnt = 1;
for(LL k=1;k<=31;k++)
{
sums *= (sta+k);
if(n % sums == 0)
{
cnt ++;
}else{
if(cnt > ansCnt)
{
ansCnt = cnt;
staNum = sta;
minSums = sums;
}
/*
else if(cnt == ansCnt)
{
if(sums < minSums)
{
staNum = sta;
minSums = sums;
}
}
*/
sta = sta + 1;//cnt;
break;
}
}
}else{
sta ++;
}
}

printf("%lld\n%lld",ansCnt,staNum);
for(LL i=staNum+1;i<staNum+ansCnt;i++)
{
printf("*%lld",i);
}
printf("\n");

}
//printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: