HDU 2710 求最大素因子

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2710

Max Factor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7668    Accepted Submission(s): 2517


Problem Description
To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as better than others. In particular, a cow whose serial number has the highest prime factor enjoys the highest social standing among all the other cows.

(Recall that a prime number is just a number that has no divisors except for 1 and itself. The number 7 is prime while the number 6, being divisible by 2 and 3, is not).

Given a set of N (1 <= N <= 5,000) serial numbers in the range 1..20,000, determine the one that has the largest prime factor.
 

Input
* Line 1: A single integer, N

* Lines 2..N+1: The serial numbers to be tested, one per line
 

Output
* Line 1: The integer with the largest prime factor. If there are more than one, output the one that appears earliest in the input file.
 

Sample Input
 
   
4 36 38 40 42
 

Sample Output
 
   
38
 

题解:此题理解题意就很简单了,意思就是求一组数据中有最大素因子的数,注意能被本身整除的素数也是素因子,1也是。比如3=3*1

还有一个坑:就是此题是多组实例,否则wa到死。

代码(此方法很巧妙,类似开灯问题):

#include
#include
using namespace std;
const int maxn = 20000+5;
int prime[maxn];
void getPrime() {
	memset(prime, 0, sizeof(prime));
	prime[1] = 1;                          ///////注意1 也为 素因子
	for (int i = 2;i < maxn;i++) {
		if (prime[i]==0) {
			for (int j = i;j < maxn;j += i)   ///////注意与求素数不同的地方,j=i   在此处wa的死
				prime[j] = i;
		}
	}
}
int main() {
	getPrime();
	int num;
	int result, max;
	while (cin >> num) {
		max = -1;
		while (num--) {
			int n;
			cin >> n;
			if (prime[n] > max) {           /////////很巧妙的方法
				result = n;
				max = prime[n];
			}	
		}
		cout << result << endl;
	}
}

一般的方法(456ms):

#include
#include
using namespace std;
const int maxn = 20000;
bool init[maxn + 5];
void getPrime() {
	memset(init, true, sizeof(init));
	for (int i = 2;i <= maxn;i++) {
		if (init[i]) {
			for (int j = i + i;j <= maxn;j += i)
				init[j] = false;
		}
	}
}
int main() {
	getPrime();
	int num;
	while (cin >> num) {
		int result, max = 0;
		while (num--) {
			int n;
			cin >> n;
			int temp = sqrt(n);  /*一定要事先求根*/
			for (int i = n;i >= temp;i--) {   /*如果此处为 i>=sqrt(n) 就超时了,因为每次判断都在求根 太费时间了*/
				if (init[i] && i> max&& n%i == 0) {
					max = i;
					result = n;
					break;
				}
			}
		}
		cout << result << endl;
	}
}


牛逼的代码分解素因子,并存起来:

#include
#include
using namespace std;
const int maxn = 20000;
bool init[maxn + 5];
int prime[maxn];
int factor[maxn][2];
void getPrime() {
	memset(init, true, sizeof(init));
	for (int i = 2;i <= maxn;i++) {
		if (init[i]) {
			for (int j = i + i;j <= maxn;j += i)
				init[j] = false;
		}
	}
	int flag = 0;
	for (int i = 2;i <= maxn;i++) {
		if (init[i])
			prime[flag++] = i;
	}
}
int cnt = 0;
int  getFactor(int n) {
	int tmp = n;
	cnt = 0;
	for (int i = 0;tmp!=1;i++) {
		while (tmp%prime[i] == 0) {
			factor[cnt][0] = prime[i];
			factor[cnt][1]++;
			tmp /= prime[i];
		}
		cnt++;
	}
	return 0;
}
int main() {
	getPrime();
	int num;
	while (cin>>num) {
		int result, max = 0;
		while (num--) {
			int n;
			cin >> n;
			if (n == 1 && n > max) {
				max = 1;
				result = 1;
				continue;
			}
			getFactor(n);
			if (factor[cnt - 1][0] > max) {
				max = factor[cnt - 1][0];
				result = n;
			}
		}
		cout << result << endl;
	}
}


你可能感兴趣的:(HDU,素数)