zoukankan      html  css  js  c++  java
  • 1059 Prime Factors (25 分)素数 “筛选法”

    1059 Prime Factors (25 分)

    Given any positive integer NNN, you are supposed to find all of its prime factors, and write them in the format NNN = p1k1×p2k2×⋯×pmkm{p_1}^{k_1} imes {p_2}^{k_2} imes cdots imes {p_m}^{k_m}p1​​k1​​​​×p2​​k2​​​​××pm​​km​​​​.

    Input Specification:

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

    Output Specification:

    Factor NNN in the format NNN = p1p_1p1​​^k1k_1k1​​*p2p_2p2​​^k2k_2k2​​**pmp_mpm​​^kmk_mkm​​, where pip_ipi​​'s are prime factors of NNN in increasing order, and the exponent kik_iki​​ is the number of pip_ipi​​ -- hence when there is only one pip_ipi​​, kik_iki​​ is 1 and must NOT be printed out.

    Sample Input:

    97532468
    

    Sample Output:

    97532468=2^2*11*17*101*1291
    思路:
      1不是素数
    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<map>
    #include<set>
    #include<cmath>
    #include<climits>
    #include<sstream>
    #include<cstdio>
    #include<string.h>
    #include<unordered_map>
    using namespace std;
    unordered_map<string,set<int>>mp;
    int main()
    {
        long long int n;
        scanf("%lld",&n);
        int num=sqrt(n);
        vector<int> primes(num,1);
        for(int i=2;i<=sqrt(num);i++)
        {
            for(int j=i+i;j<=sqrt(num);j+=i)
                primes[j]=0;
        }
        if(n==1)
            printf("1=1");
        else
        {
            printf("%lld=",n);
            int state=0;
            for(int i=2;i<=num;i++)
            {
                int cnt=0;
                bool flag=false;
                while(primes[i]==1&&n%i==0)
                {
                    n/=i;
                    cnt++;
                    flag=true;
                }
                if(flag)
                {
                    if(state!=0) printf("*");
                    state=1;
                    printf("%d",i);
                    if(cnt>1)
                        printf("^%d",cnt);
                }
                if(n==1)
                    break;
            }
            if(n!=1)
            {
                if(state!=0) printf("*");
                printf("%d",n);
            }
        }
        return 0;
    }
     
  • 相关阅读:
    Ambari源代码分析之总览
    最简单的修改HashMap value值的方法
    机器学习 Hidden Markov Models 1
    OpenCV坐标系与操作像素的四种方法
    OpenCV2.4.13+VS2012开发环境配置
    OpenCV——PS滤镜算法之 Ellipsoid (凹陷)
    OpenCV——PS滤镜算法之 Ellipsoid (凸出)
    如何快糙好猛的使用Shiqi.Yu老师的公开人脸检测库(附源码)
    伊斯坦布尔的流浪 (三)
    伊斯坦布尔的流浪 (一)
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10346326.html
Copyright © 2011-2022 走看看