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. Linux ALSA声卡驱动之七:ASoC架构中的Codec

    1.  Codec简介(ad/da) 在移动设备中,Codec的作用可以归结为4种,分别是: 对PCM等信号进行D/A转换,把数字的音频信号转换为模拟信号 对Mic.Linein或者其他输入源的模拟信 ...

  2. win7下远程登录ubuntu mysql

    网络上找了很久的一个办法,不然老是远程访问不了linux mysql. 原先一直用root登录,进不了,新建一个root1倒是可以了. 安装好mysql后,按以下步骤: 1.将vim /etc/mys ...

  3. LCA__st算法&&树上倍增

    st表 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; ]; ] ...

  4. bzoj4756

    http://www.lydsy.com/JudgeOnline/problem.php?id=4756 水题一枚...但是我写了一个小时...手贱打反查不出来... 就是每次线段树合并,先把自己的儿 ...

  5. Kafka VS Flume

     (1)kafka和flume都是日志系统.kafka是分布式消息中间件,自带存储,提供push和pull存取数据功能.flume分为agent(数据采集器),collector(数据简单处理和写入) ...

  6. windows下写的脚本,在linux下执行失败

    Windows中的换行符为CRLF, 即正则表达式的rn(ASCII码为13和10), 而Unix(或Linux)换行符为LF, 即正则表达式的n. 在Windows和Linux下协同工作的时候, 往 ...

  7. Linux系统的整体目录结构和文件解析

    Linux系统目录结构 使用 ls / 查看系统的文件目录: /:根目录,根目录下一般只存放子目录,不存放文件.在linux系统中所有的文件都挂载该目录下. /bin:命令目录. 存放系统的可执行的二 ...

  8. Java基础学习经验分享

    很多人学习Java,尤其是自学的人,在学习的过程中会遇到各种各样的问题以及难点,有时候卡在一个点上可能需要很长时间,因为你在自学的过程中不知道如何去掌握和灵活运用以及该注意的点.下面我整理了新手学习可 ...

  9. Spring Boot (26) RabbitMQ延迟队列

    延迟消息就是指当消息被发送以后,并不想让消费者立即拿到消息,而是等待指定时间后,消费者才拿到这个消息进行消费. 延迟队列 订单业务: 在电商/点餐中,都有下单后30分钟内没有付款,就自动取消订单. 短 ...

  10. 消息队列 (2) java实现简单的RabbtMQ

    假设有如下问题: 1.如果消费者连接中断,这期间我们应该怎么办? 2.如何做到负载均衡? 3.如何有效的将数据发送到相关的接收者?就是怎么样过滤 4.如何保证消费者收到完整正确的数据 5.如何让优先级 ...