1096. Consecutive Factors (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 思路
因为12! < 2^31 < 13!。
所以,因子大小不会超过12!,连续因子的长度不会超过12。
从长度为12开始减小,令res为满足条件的最大因子,穷举所有可能直到满足N % res == 0就行。
注:穷举的过程中res会溢出,不过不影响结果。。
代码
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int N;
while(cin >> N)
{
int maxnum = sqrt(N);
for(int len = 12;len >= 1;len--)
{
for(int start = 2; start <= maxnum;start++)
{
int res = 1;
for(int i = start;i - start < len;i++)
{
res *= i;
}
if(N % res == 0)
{
cout << len << endl << start;
for(int j = start + 1;j - start < len;j++)
cout << "*" << j;
return 0;
}
}
}
cout << 1 << endl;
cout << N;
}
}

  

最新文章

  1. Oracle交易流水号问题
  2. 2017微软秋招A题
  3. js加解密
  4. python环境搭建-Pycharm 调整字体大小
  5. php部分---创建连接数据库类
  6. jquery表单验证使用插件formValidator
  7. php制作数据字典
  8. 超棒的自定义超酷滚动条jQuery插件 - Perfect Scrollbar
  9. 【python】函数参数关键字索引、参数指定默认值、搜集参数
  10. UEP-时间的比较
  11. paramiko__摘抄
  12. JVM(一)
  13. dubbo实现原理之动态编译
  14. [Go] defer 语句
  15. 浅谈压缩感知(七):常见测量矩阵的MATLAB实现
  16. java修饰符的作用范围
  17. 通过编写聊天程序来熟悉python中多线程及socket的用法
  18. [翻译] NSImage+HHTint - Tints grayscale images using CoreImage
  19. A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28
  20. dubbo注册中心zookeeper出现异常 Opening socket connection to server 10.70.42.99/10.70.42.99:2181. Will not attempt to authenticate using SASL (无法定位登录配置)

热门文章

  1. Android和iOS中Cocos2D日志为什么会出现skip frames
  2. Leetcode_94_Binary Tree Inorder Traversal
  3. AngularJS进阶(九)控制器controller之间如何通信
  4. 关于C++“加、减机制”的整理
  5. android bitmap的内存分配和优化
  6. android 自定义gallerey并实现预览功能
  7. Socket层实现系列 — listen()的实现
  8. web报表工具FineReport常用函数的用法总结(日期和时间函数)
  9. 使用Interlocked在多线程下进行原子操作,无锁无阻塞的实现线程运行状态判断
  10. 从小故事来谈nginx负载均衡