(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, 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


Author

Wiskey


Source

HDU 2007-11 Programming Contest_WarmUp


Recommend

威士忌

题目分析:

求一个数的最大质因子的位置。例如2的最大质因子是2,它是第一个素数,所以它的最大质因子的位置就是1.同理可得3的最大质因子的位置是2.这道题采用预先打表的方式来做。

1)从小到大遍历数据范围内的所有数。把包含质因子的数的位置都设成跟质因子的位置相同。

2)同一个数的位置可能被多次复写。但是由于是从小到大遍历,这就保证了最后一次写入的是该数的最大质因子的位置

代码如下:

/*
 * c1.cpp
 *
 *  Created on: 2015年1月30日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>

using namespace std;

const int maxn = 1000005;
int biao[maxn];

/**
 * 求一个数的最大质因子的位置。如2的位置是1。
 * 3的位置是2
 */
void prepare(){
	int i;
	int k = 1;
	for(i = 2 ; i < maxn ; ++i){//遍历数据范围内的所有数
		if(biao[i] == 0){//如果这一个数的最大质因子的位置还没有确定
			int j;
			for(j = 1 ; i*j < maxn ; ++j){//把含有这个质因子的所有数的位置都标记成这个质因子的位置
				biao[i*j] = k;
			}
			k++;//质因子的位置索引+1
		}
	}
}

int main(){
	int n;
	prepare();
	while(scanf("%d",&n)!=EOF){
		printf("%d\n",biao[n]);//biao[n]表示n的最大质因子的位置
	}

	return 0;
}
时间: 2024-10-08 15:17:52

(hdu step 2.1.3)Largest 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): 7009    Accepted Submission(s): 2482 Problem Description Everybody knows any number can be combined by the prime number. Now

HDU 2136 Largest prime factor (最大素因子序号,cin超时呀!!!)

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

HDU 2136 Largest prime factor 数论

Largest prime factor Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

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

2136 Largest 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 E

Largest prime factor

problem 3:Largest prime factor 题意:求600851475143的最大的质因数 代码如下: 1 #ifndef PRO3_H_INCLUDED 2 #define PRO3_H_INCLUDED 3 4 #include "prime.h" 5 6 namespace pro3{ 7 long long solve(){ 8 long long n=600851475143LL,maxn=0;; 9 for(long long i=1;i*i<=n;

The largest prime factor(最大质因数)

1. 问题: The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 600851475143 ? 2. 解法(by java in Eclipse) 1 package com.lun.alrithmetic; 2 /* 3 * Q1: what's the primary factor? (1 2 3) 4 * Q2: i & i+2ne能否遍历出所有质数 5

HDU 2136 Largest prime factor

水题,晒一遍素数并标注就OK了. #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #include<iostream> #include<list> #include<set> #include<

HDU 2136 Largest prime factor (素数打表。。。)

题意:给你一个数,让你求它的最大因子在素数表的位置. 析:看起来挺简单的题,可是我却WA了一晚上,后来终于明白了,这个第一层循环不是到平方根, 这个题和判断素数不一样,只要明白了这一点,就很简单了. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring>