hdu2136Largest prime factor (关建在求素数,有点意思的题)

Problem Description

Everybody knows any number can be combined by the prime number.

Now, your task is telling me what position of the largest prime factor.

The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.

Specially, LPF(1) = 0.

Input

Each line will contain one integer n(0 < n < 1000000).

Output

Output the LPF(n).

Sample Input

1
2
3
4
5

Sample Output

0
1
2
1
3

题意:求一个数的最简因式中最大素数因子在所有的素数中位置,位置从0开始。

分析:所有的数都可以用素数的倍数来表示,原因:数分为两类:素数和非素数。非素数那么一定可以分解得到最简因式,素数不能分解,其最简因式是本身。

综上所述:一个数的最简因式中因子一定全是素数。

方法一: 
#include<stdio.h>
int prim[1000005]={0};
void init()
{
    int k;
    prim[1]=0; k=1;
    for(int i=2;i<1000000;i++)
    if(prim[i]==0)//i是素数
    {
      for(int j=i; j<1000000; j+=i)
        prim[j]=k;
        k++;
    }
}
int main()
{
    int n;
    init();
    while(scanf("%d",&n)>0)
        printf("%d\n",prim[n]);
}

方法二:

#include<stdio.h>
int vist[1000005]={0},prim[1000005];
void init()
{
    int k;
    prim[1]=1; prim[2]=2;k=3;
    for(int i=3;i<1000000;i+=2)
    if(vist[i]==0)
    {
        prim[i]=k; k++;
        for(int j=i; j<1000000; j+=i)
        if(vist[j]==0)
        vist[j]=1;
    }
}
int main()
{
    int n,posit;
    init();
    while(scanf("%d",&n)>0)
    {
        posit=1;
        for(int i=2; ; i++)
            if(prim[n]==0)
            {
                if(prim[i]&&n%i==0)posit=i;
                while(n%i==0)n/=i;
            }
            else
            {
               if(n>posit)posit=n; break;
            }
            printf("%d\n",prim[posit]-1);
    }
}

hdu2136Largest prime factor (关建在求素数,有点意思的题)

时间: 2024-11-10 11:19:43

hdu2136Largest prime factor (关建在求素数,有点意思的题)的相关文章

HDU 2136 Largest prime factor (筛选法求素数)

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7297    Accepted Submission(s): 2589 Problem Description Everybody knows any number can be combined by the prime number. Now

hdu 2136 (Largest prime factor)就是简单 的筛选素数法

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7009    Accepted Submission(s): 2482 Problem Description Everybody knows any number can be combined by the prime number. Now

HDU2136_Largest prime factor【水题】【筛法求素数】

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7216    Accepted Submission(s): 2559 Problem Description Everybody knows any number can be combined by the prime number. Now

HDU2710_Max Factor【水题】【筛法求素数】

Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3966    Accepted Submission(s): 1289 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1

(hdu step 2.1.3)Largest prime factor(求一个数的最大质因子的位置)

题目: Largest prime factor Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4868 Accepted Submission(s): 1452   Problem Description Everybody knows any number can be combined by the prime number.Now,

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Accepted: 10619 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations d

JD 题目1040:Prime Number (筛法求素数)

OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下的数中选择最小的数是素数,然后去掉它的倍数. 依次类推.直到筛子为空时结束. 如有: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1不是素数.去掉.剩下的数中2最小,是素数,去掉2的

hdu how many prime numbers 筛选法求素数

/* * hdu How many prime numbers * date 2014/5/13 * state AC */ #include <iostream> #include <cmath> #include <cstdio> using namespace std; bool isPrime(int x) { int sqr=sqrt(x*1.0); for(int i=2;i<=sqr;i++) { if(x%i==0)return false; }

poj 2689 Prime Distance 【数论】【筛法求素数】

题目链接:传送门 题目大意: 给你L和R两组数,L和R的范围是2^32,其间隔(即R-L最大为1,000,000.) .让你求出L和R之间素数的最大间隔和最小的间隔. 比如 2 17.之间的最小素数间隔是2 3,最大的素数间隔是11 17. 要是直接进行一个2^32次方筛法然后在判断是会T的. 我们这样来想,筛法求素数的原理是什么: /**vis数组标记为0则说明是素数*/ int vis[10005]; void getPrimevis(int n) { int m=sqrt(n+0.5);