PAT (Advanced Level) Practice 1059 Prime Factors(25分)【数学:素数】

Given any positive integer N N , you are supposed to find all of its prime factors, and write them in the format N = p 1 k 1 × p 2 k 2 × × p m k m N=p_1^{k_1}×p_2^{k_2}×⋯×p_m^{k_m} .

Input Specification:

Each input file contains one test case which gives a positive integer N N in the range of long int.

Output Specification:

Factor N in the format N N = p 1 p_1 ^ k 1 k_1 * p 2 p_2 ^ k 2 k_2 * * p m p_m ^ k m k_m , where p i p_i 's are prime factors of N N in increasing order, and the exponent k i k_i is the number of p i p_i – hence when there is only one p i p_i , k i k_i is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291

题意

将一个数进行质因数分解。

思路

首先要算质数,题中的数据范围是long int,也就是 2 3 2 2^32 。由于 2 1 6 = 65536 2^16=65536 ,所以只需要算出100000以内的全部质数即可。得到质数后用这些质数中能整除的去除x,直到x=1。

有可能全部的质数都除过后x还是不为1,这时的x是一个很大的质数。但是数据很弱,不考虑这种情况也能过。

注意边界情况x=1。

代码

#include <iostream>

using namespace std;
#define MAX_N 100000

bool not_prime[MAX_N];

int main() {
    int x;
    cin >> x;
    cout << x << "=";
    if (x == 1)
        cout << "1";

    for (int i = 2; i < MAX_N; ++i)  // 欧拉筛
        if (!not_prime[i])
            for (int j = i + i; not_prime[j / i]; j += i)
                not_prime[j] = true;

    bool first = true;
    for (int i = 2; i < MAX_N && x != 1; ++i) {
        if (!not_prime[i]) {
            int cnt = 0;
            while (x % i == 0) {
                x /= i;
                ++cnt;
            }
            if (cnt > 0) {
                if (first)
                    first = false;
                else
                    cout << '*';
                cout << i;
                if (cnt > 1)
                    cout << '^' << cnt;
            }
        }
    }
    if (x != 1)
        cout << '*' << x;
}

发布了184 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Exupery_/article/details/104167812