Nine degrees OJ 1207 mass factor number (sieve prime, barely AC)

Source: Internet
Author: User

1 seconds

Memory limit:32 MB

Special question: No

submitted:4466

Resolution:1375

Title Description:
The
number of the mass factor for a positive integer N (n>1). The same quality factor requires repeated calculations. such as 120=2*2*2*3*5, a total of 5 qualitative factors.
Input:

There may be multiple sets of test data, and the input to each set of test data is a positive integer N, (1<n<10^9).

Output:

For each set of data, the number of the quality factor of the output n.

Sample input:
120
Sample output:
5
Tips:

Note: 1 is not a mass factor of N; n is the mass factor of N if n is a prime number.

Source:
2007, Tsinghua University computer Research Life Test real problem

If all the factors in the test 2 to 100000 are not decomposed to 1, a also has a factor greater than 100000 ans++; Solution 1:

#include <stdio.h> #define M 100000int prime[m];int flag[m];int cnt;void getprime () {    cnt=1;    for (int i=2;i<m;++i)    {        if (!flag[i])        {            prime[cnt++]=i;            for (int j=i;j<m;j+=i)                flag[j]=1;}}    } int main (int argc, char *argv[]) {    //freopen ("1207.in", "R", stdin);    int num;    Getprime ();    while (scanf ("%d", &num)!=eof)    {        int ans=0;        int i;        for (I=1;i<cnt;++i) {            while (num%prime[i]==0) {                ans++;                Num/=prime[i];            }            if (num==1) break;        }        if (i==cnt)           ans++;        printf ("%d\n", ans);    }    return 0;}

Solution 2:

#include <iostream> #include <stdio.h>using namespace std;int main (int argc, char *argv[]) {    int num;    Freopen ("1207.in", "R", stdin);    while (scanf ("%d", &num)!=eof)    {        int ans=0;        int i=2;        int n=num;        while (i*i<=n) {            if (num%i==0)            {                num/=i;                ans++;            }            else                i++;        }        if (num!=1)            ans++;        printf ("%d\n", ans);    }    return 0;}

Time Comparison--

Solution One:

Solution Two:


I don't see the difference.

Nine degrees OJ 1207 mass factor number (sieve prime, barely AC)

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.