Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 356*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 Specification:

Each input file contains one test case, which gives the integer N (1<N<231).

Output Specification:

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

567

分析

这道题自己的解法超时了,于是看了别人的解法,最后输出的长度是不会超过12的,因为13的阶乘大于2^31了,并且可以知道如果长度大于等于2的话,那么第一个数是一定小于sqrt(n)的,否则乘积一定大于n.

附上别人的解析:

用暴力破解,一个个地尝试呀既然是递增连续的因子长度那么肯定是不重复的几个连着的数字相乘咯然后就想到了阶乘的概念对不对,首先题目说了n最大就2的31次方,后来我掐指一算就知道了2的31次方是介于12的阶乘和13的阶乘之间的大小的也就是说~~所求连续因子的长度再怎么长,也长不过12个数字那就从len=12开始,一直到len=1,看看能不能找到一个满足条件的序列咯

假如长度比1大那么至少得两个相邻的数相乘那么这两个数的乘积是不可能超过n的~【就是说如果不止一个因子相乘,那么所求因子序列的第一个数字不可能超过sqrt(n)】那就从start = 2开始(和从1开始相乘是一个意思啦= =乘不乘以1不都一样嘛= =)一直到start = sqrt(n),把start * (start+ 1)(start+2)…【嗯对一共要乘以len的长度个因为我们现在在尝试是否有连续的len长度的因子相乘满足条件】,乘完了的结果ans看看是不是n的约数【就是看看n%ans是不是等于0咯】,等于0就好办啦,说明已经找到这个最长的连续因子啦~快输出.然后直接return.耶耶耶.要是没找到,那说明什么呢。。说明它是个质数(就是说说明它因子只有他自己本身),那就输出长度为1,而且因子就是n本身咯

你可能会想。。既然len=1为什么要算?因为题目里说输出最小的连续因子序列,那么如果这个数不是质数,也就是说在检验len=1的时候发现了一个比这个数小的因子x,那个这个x就是最小的因子啦,就不能够是等到最后一句输出n为最小的因子啦,如果n是质数,那么小于sqrt(n)的限定情况下会保证它不找到n的本身这个数的,所以到最后要加上printf(“1\n%d”, n);哦,保证它是质数的时候也能被输出答案.

#include<iostream>
#include<math.h>
using namespace std;
int main(){
long long int n;
cin>>n;
long long int max=sqrt(n);
for(int len=12;len>=1;len--)
for(int start=2;start<=max;start++){
long long int ans=1;
for(int i=start;i-start<=len-1;i++)
ans*=i;
if(n%ans==0){
printf("%d\n%d",len,start);
for(int i=start+1;i-start<=len-1;i++)
printf("*%d",i);
return 0;
}
}
printf("1\n%d",n);
return 0;
}

PAT 1096. Consecutive Factors的更多相关文章

  1. PAT 1096 Consecutive Factors[难]

    1096 Consecutive Factors (20 分) Among all the factors of a positive integer N, there may exist sever ...

  2. PAT甲级——1096 Consecutive Factors (数学题)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91349859 1096 Consecutive Factors  ...

  3. PAT (Advanced Level) Practise - 1096. Consecutive Factors (20)

    http://www.patest.cn/contests/pat-a-practise/1096 Among all the factors of a positive integer N, the ...

  4. PAT 甲级 1096 Consecutive Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...

  5. PAT Advanced 1096 Consecutive Factors (20) [数学问题-因子分解 逻辑题]

    题目 Among all the factors of a positive integer N, there may exist several consecutive numbers. For e ...

  6. 1096. Consecutive Factors (20)

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...

  7. PAT A1096 Consecutive Factors (20 分)——数字遍历

    Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...

  8. PAT (Advanced Level) 1096. Consecutive Factors (20)

    如果是素数直接输出1与素数,否则枚举长度和起始数即可. #include<cstdio> #include<cstring> #include<cmath> #in ...

  9. PAT甲题题解-1096. Consecutive Factors(20)-(枚举)

    题意:一个正整数n可以分解成一系列因子的乘积,其中会存在连续的因子相乘,如630=3*5*6*7,5*6*7即为连续的因子.给定n,让你求最大的连续因子个数,并且输出其中最小的连续序列. 比如一个数可 ...

随机推荐

  1. Scikit-learn库中的数据预处理:独热编码(二)

    在上一篇博客中介绍了数值型数据的预处理但是真实世界的数据集通常都含有分类型变量(categorical value)的特征.当我们讨论分类型数据时,我们不区分其取值是否有序.比如T恤尺寸是有序的,因为 ...

  2. IDEA 中Spark SQL通过JDBC连接mysql数据库

    一.IDEA装驱动: 1.下载一个MySQL的JDBC驱动:mysql-connector-java-5.1.44.tar.gz2.在idea Open Moudle Settings 在 Moudl ...

  3. 【TODO】Ruby Range类

    创建: 2017/10/28   Range类                                                                             ...

  4. Mac OS下配置 ADB环境变量

    前提已经安装了Android sdk. 步骤打开终端Terminal, 输入open -e ~/.bash_profile, 若之前没有该文件,会自动创建.添加内容 export PATH=${PAT ...

  5. 【洛谷3321_BZOJ3992】[SDOI2015]序列统计(原根_多项式)

    题目: 洛谷3321 分析: 一个转化思路比较神(典型?)的题-- 一个比较显然的\(O(n^3)\)暴力是用\(f[i][j]\)表示选了\(i\)个数,当前积在模\(m\)意义下为\(j\)的方案 ...

  6. Spring思维课程导图——bean得实例化和bean的管理

  7. mybatis中打印sql语句

    在mybatis-config.xml中properties节点下,配置一个settings节点 <settings> <setting name="cacheEnable ...

  8. conda python虚拟环境

    #查看已安装的python包 conda list #查看当前有哪些虚拟环境 conda env list 或者 conda info -e #更新conda conda update conda # ...

  9. 实现微信小程序的wxml文件和wxss文件在phpstrom的支持

    最近下载了微信小程序准备好好看看,但是发现微信小程序用的后缀名是不一样的,.wxml代表的就是平时用的.html,.wxss代码的就是平时用的.css.但是phpstorm无法识别,为了更方便的码代码 ...

  10. 03-Servlet 体系结构知识梳理

    一.Servlet体系结构 Java Web应用是基于Servlet规范运行,Servlet顶层类的关联如下图: 从图可看出,Servlet规范基本围绕这几个类运行,其中,与Servlet主动关联的有 ...