zoukankan      html  css  js  c++  java
  • PAT 甲级 1059 Prime Factors

    https://pintia.cn/problem-sets/994805342720868352/problems/994805415005503488

    Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1​​k1​​​​×p2​​k2​​​​××pm​​km​​​​.

    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 = 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

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    long int P;
    int prime[500010];
    
    int main() {
        memset(prime, 1, sizeof(prime));
        for(int i = 2; i * i <= 500010; i ++)
            for(int j = 2; j * i < 500010; j ++)
            prime[j * i] = 0;
        scanf("%ld", &P);
    
        printf("%ld=", P);
        if(P == 1) printf("1");
    
        bool First = false;
        for(int i = 2; P >= 2; i ++) {
            int cnt = 0, flag = 0;
            while(prime[i] && P % i == 0) {
                cnt ++;
                P /= i;
                flag = 1;
            }
            if(flag) {
                if(First)
                    printf("*");
                printf("%d", i);
                First = true;
            }
            if(cnt > 1)
                printf("^%d", cnt);
        }
        return 0;
    }
    

      素数表! Get

  • 相关阅读:
    HDOJ 1241 Oil Deposits【最大连通块 dfs】
    POJ 3984 迷宫问题【迷宫最短路径 bfs】
    封装
    继承的另一种使用方式。。。
    类的绑定方法与继承
    XML模块与类的定义
    常用模块三
    python day19
    常用模块与项目目录规范
    python day17
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10185932.html
Copyright © 2011-2022 走看看