Topic Information
1096. Consecutive factors (20)
Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Among all the factors of a positive an integer N, there may exist several consecutive numbers. For example, 630 can is factored as 3*5*6*7, where 5, 6, and 7 are the three consecutive numbers. Now given any positive N, you is supposed to find the maximum number of consecutive factors, and list the smallest Sequen Ce of the consecutive factors.
Input Specification:
Each input file contains one test case, which gives the integer N (1
Thinking of solving problems
Discussion on the two cases of odd and even
AC Code
#include <algorithm>#include <set>#include <queue>#include <map>#include <stdio.h>#include <iostream>#include <string>#include <memory.h>#include <limits.h>using namespace STD;voidDfsfactor (Long LongNLong LongPrenum, vector<int>&factor, vector<vector<int>>&ans,int&factorsize) {if(n% (Prenum +1) ==0) {Factor.push_back (Prenum +1); Dfsfactor (n/(Prenum +1), Prenum +1, factor, ans, factorsize); }Else if(!factor.empty () && factor.size () > Factorsize) {factorsize = Factor.size (); Ans.push_back (factor); }}BOOLcmpConst vector<int>&a,Const vector<int>&B) {if(A.size () > B.size ())return true;Else if(a.size () = = B.size () && a[0] < b[0])return true;Else return false;}intMainvoid){intNCin>> N;if(n%2==1) {inttemp = (int)((Double)sqrt(n) +1); for(inti =2; I <= temp; ++i) {if(n%i = =0) {cout<<"1"<< Endl;cout<< i << Endl;return 0; } }cout<<"1"<< Endl;cout<< n << Endl; }Else{ vector<int>Maxfactor = {Int_max, n, (int)((Double)sqrt(n) +1),1290,215, the, *, +, -,Ten,0};//3~13 vector<int>Factor0); vector<vector<int>>Ans0);intFactorsize =0; for(Long Longi =1; I <= min (n, maxfactor[factorsize +1]); i++) {if(n% (i +1) ==0) {Dfsfactor (n, I, factor, ans, factorsize); Factor.clear (); }} sort (Ans.begin (), Ans.end (), CMP);cout<< ans[0].size () << Endl; for(inti =0; I < ans[0].size (); i++) {cout<< ans[0][i];if(I! = ans[0].size ()-1)cout<<"*"; }cout<< Endl; }return 0;}
1096. Consecutive factors (--PAT) practise