2-07. prime factor decomposition (20) (zjupat mathematics)

Source: Internet
Author: User

Link: http://pat.zju.edu.cn/contests/ds/2-07


Given a positive integer N, obtain the factorization result of the prime factor, that is, the factorization expression n = p1 ^ K1 * P2 ^ K2 *... * PM ^ km.

Input format description:

Enter a positive integer N in the long int range.

Output format description:

Output n factorization expressions in the given format, that is, n = p1 ^ K1 * P2 ^ K2 *... * PM ^ km, where Pi is a prime factor and must be output from small to large. The exponent Ki is the number of Pi. When Ki = 1, that is, if the factor Pi has only one, no Ki is output.

Sample input and output:

Serial number Input Output
1
1024
1024=2^10
2
1323
1323=3^3*7^2
3
97532468
97532468=2^2*11*17*101*1291
4
1
1=1
5
3
3=3


The Code is as follows:

#include <cstdio>#include <cstring>#include <cmath>#define LL intconst int MAXN = 11117;int main(){    LL n;    int p[MAXN], k[MAXN];    while(~scanf("%d",&n))    {        LL tt = n;        memset(p,0,sizeof(p));        memset(k,0,sizeof(k));        int i = 2;        int cont = 0;        int flag = 0;        int l = 0;        if(n == 1)        {            printf("1=1\n");            continue;        }        while(n!=1)        {            while(n%i==0)            {                flag = 1;                cont++;                n /= i;            }            if(flag)            {                p[l] = i;                k[l] = cont;                l++;                flag = 0;                cont = 0;            }            i++;        }        printf("%d=",tt);        for(int i = 0; i < l-1; i++)        {            if(k[i] == 1)                printf("%d*",p[i]);            else                printf("%d^%d*",p[i],k[i]);        }        if(k[l-1] == 1)            printf("%d\n",p[l-1]);        else            printf("%d^%d\n",p[l-1],k[l-1]);    }    return 0;}


2-07. prime factor decomposition (20) (zjupat mathematics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.