1. 程式人生 > 其它 >PTA連續因子

PTA連續因子

一、題目描述

 

 二、解題思路

  我們列舉n的因子,然後列舉這個因子的最大長度,然後做個標記。最後在輸出答案的時候反推就可以了。

三、程式碼實現

 1 #include "bits/stdc++.h"
 2 #define PII pair<int,int>
 3 #define rep(i,z,n) for(int i = z;i <= n; i++)
 4 #define per(i,n,z) for(int i = n;i >= z; i--)
 5 #define ll long long
 6 #define db double
 7 #define vi vector<int>
 8
#define debug(x) cerr << "!!!" << x << endl; 9 using namespace std; 10 inline ll read() 11 { 12 ll s,r; 13 r = 1; 14 s = 0; 15 char ch = getchar(); 16 while(ch < '0' || ch > '9'){ 17 if(ch == '-') 18 r = -1; 19 ch = getchar(); 20 }
21 while(ch >= '0' && ch <= '9'){ 22 s = (s << 1) + (s << 3) + (ch ^ 48); 23 ch = getchar(); 24 } 25 return s * r; 26 } 27 inline void write(ll x) 28 { 29 if(x < 0) putchar('-'),x = -x; 30 if(x > 9) write(x / 10); 31 putchar(x % 10 + '
0'); 32 } 33 bool prime(int k) 34 { 35 if(k == 1) 36 return false; 37 for(int i = 2;i <= sqrt(k);i++) 38 if(k % i == 0) 39 return false; 40 return true; 41 } 42 int n; 43 int maxsize; 44 int pos = 0; 45 void solve(){ 46 for(int i = 2;i <= sqrt(n);i++){ 47 int j = i; 48 int cnt = 0; 49 int tt = n; 50 while(tt % j == 0){ 51 tt /= j; 52 cnt++; 53 j++; 54 } 55 if(cnt > maxsize){ 56 pos = i; 57 maxsize = cnt; 58 } 59 } 60 } 61 int main() 62 { 63 n = read(); 64 if(prime(n)){ 65 cout << 1 << endl; 66 cout << n << endl; 67 return 0; 68 } 69 solve(); 70 if(maxsize){ 71 cout << maxsize << endl; 72 for(int i = 1;i <= maxsize;i++){ 73 if(i == 1) 74 cout << pos; 75 else 76 cout << '*' << pos + i - 1; 77 } 78 } 79 return 0; 80 }