PAT 1059 Prime Factors[難]
阿新 • • 發佈:2018-11-10
1059 Prime Factors (25 分)
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1k1×p2k2×⋯×pmkm.
Input Specification:
Each input file contains one test case which gives a positive integer N in the range of long int.
Output Specification:
Factor N in the format N =
p1^
k1*
p2^
k2*
…*
pm^
km, where pi's are prime factors of N in increasing order, and the exponent ki is the number of pi -- hence when there is only one pi, ki is 1 and must NOT be printed out.
Sample Input:
97532468
Sample Output:
97532468=2^2*11*17*101*1291
題目大意:給出一個數n,在long int內,給出其素數分解,如果某個素數有多個是其分數,那麼用指數形式表示;如果n本身就是素數,那麼指數1不輸出。
//猛一看,自己就想到了是建立大數素數表,但是具體操作我似乎不大會。以前看過,但是不知道具體怎麼寫了。
//自己寫了一點點就蔫了。 #include <iostream> #include <algorithm> #include<cstdio> #include<stdio.h> #include <map> #include<cmath> using namespace std; //需要先求出long int內所有的素數。 //但是有一個問題,如果是long int,那麼我定義多大的陣列呢?要麼用向量? #define maxn 1e7 int a[maxn]; vector<int> vt; bool isPrime(int n){ int m=sqrt(n); for(int i=2;i<=n;i++){ if(n%i==0)return false; } return true; } void getPrime(int n){ for(int i=2;i<=n;i++){ if(a[i]==0){ if(isPrime(i)){ a[i]=1; for(int j=i*i;j<=n;j=j*i){//是按照立方去標記嗎? } } } } } int main() { long int n; cin>>n; getPrime(n); return 0; }
下面是柳神的程式碼:
1.原來求大數內素數還可以這麼求,學習了。
2.並且在判斷時可以邊判斷邊輸出。