码迷,mamicode.com
首页 > 其他好文 > 详细

1096. Consecutive Factors (20)

时间:2015-12-06 12:54:37      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

n和i要用long long要不然乘着乘着就是负的了

时间限制
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

 

  1. #include<iostream>
  2. #include <vector>
  3. #include <math.h>
  4. using namespace std;
  5. int maxCnt = 0;
  6. int startIndex;
  7. int main(void) {
  8. long long int n, cnt, ntemp;
  9. cin >> n;
  10. for (long long int i = 2; i*i<n&&pow(i, maxCnt) <= n; i++) {
  11. if (n%i == 0) {
  12. cnt = 1;
  13. ntemp = n / i;
  14. int j = i+1;
  15. while (true)
  16. {
  17. if (cnt > maxCnt) {
  18. maxCnt = cnt;
  19. startIndex = i;
  20. }
  21. if (ntemp%j == 0) {
  22. ntemp = ntemp / j;
  23. j++;
  24. cnt++;
  25. }
  26. else {
  27. break;
  28. }
  29. }
  30. //i = j-1;
  31. }
  32. }
  33. if (maxCnt == 0) {
  34. cout << "1" << endl << n;
  35. return 0;
  36. }
  37. cout << maxCnt << endl;
  38. for (int i = 0; i < maxCnt; i++) {
  39. if (i != maxCnt - 1)
  40. cout << startIndex + i << "*";
  41. else
  42. cout << startIndex + i;
  43. }
  44. return 0;
  45. }





1096. Consecutive Factors (20)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!