题目
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 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
5*6*7
思路
这道题远没有想象的简单,应该值25分甚至30分。
最初的想法是对n因式分解,630 = 2 * 2 * 3 * 5 * 7,接下来就傻了。例子中630 = 3 * 5 * 6 * 7,并未再对6分解。
没有想到简便的方法,学习了柳神的代码,主要思路是:
- n的因子最大为sqrt(n),可令i从2到sqrt(n)不断构造连乘,直到乘积不能整除n为止,此时得到的就是从i开始的最大连续因子;记录其中长度最大的即可。
- 由于n<231ÿ