UVa 11466 - Largest Prime Divisor

題目:給你一個整數n(不超過14位)。求出他的最大的素數因子。假设仅仅有一個素數因子輸出-1。

分析:數論。

直接打表計算10^7內的全部素數因子,然後用短除法除n。記錄最大的因子就可以。

假设最後下的數字不是1,則它就是最大的素數因子。

說明:注意n可能為負數。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int visit[10000001];
int prime[10000001];

int main()
{
    memset(visit, 0, sizeof(visit));
    int count = 0;
    for (int i = 2; i < 10000001; ++ i) {
        if (!visit[i]) prime[count ++] = i;
        for (int j = 0; j < count && i*prime[j] < 10000001; ++ j)
            visit[i*prime[j]] = 1;
    }

	long long n,m;
	while (cin >> n && n) {
		if (n < 0) n = -n;
		int size = 0;
		for (int i = 0; i < count; ++ i) {
			if (n < prime[i]) break;
			if (n%prime[i] == 0) {
				m = prime[i];
				++ size;
				while (n%m == 0) n /= m;
			}
		}
		if (n != 1) {
			m = n;
			++ size;
		}

		if (size > 1)
			cout << m << endl;
		else cout << "-1" << endl;
	}
    return 0;
}
时间: 2024-10-18 16:20:45

UVa 11466 - Largest Prime Divisor的相关文章

UVA - 10780 Again Prime? No Time. (质因子分解)

Description Again Prime? No time. Input: standard input Output: standard output Time Limit: 1 second The problem statement is very easy. Given a number n you have to determine the largest power of m, not necessarily prime, that divides n!. Input The

uva 1415 - Gauss Prime(高斯素数)

题目链接:uva 1415 - Gauss Prime 题目大意:给出一个a,b,表示高斯数a+bi(i=?2 ̄ ̄ ̄√,推断该数是否为高斯素数. 解题思路: a = 0 时.肯定不是高斯素数 a != 0时,推断a2+2b2是否为素数就可以. #include <cstdio> #include <cstring> #include <cmath> bool is_prime (int n) { int m = sqrt(n+0.5); for (int i = 2;

uva 11610 Reverse Prime

Problem FReverse Prime Input: Standard Input Output: Standard Output There are a few 7 digit positive numbers whose reverse number is a prime number and less than 10^6.  For example: 1000070, 1000090 and 1000240 are first few reverse prime numbers be

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;

uva 1463 - Largest Empty Circle on a Segment(二分+三分+几何)

题目链接:uva 1463 - Largest Empty Circle on a Segment 二分半径,对于每个半径,用三分求出线段到线段的最短距离,根据最短距离可以确定当前R下每条线段在[0,L]上的可行区间,存在一个点被可行区间覆盖n次. #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <algorithm> using n

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

uva 10539 - Almost Prime Numbers(数论)

题目链接:uva 10539 - Almost Prime Numbers 题目大意:给出范围low~high,问说在这个范围内有多少个数满足n=pb,(p为素数). 解题思路:首先处理出1e6以内的素数,然后对于每个范围,用solve(high)?solve(low?1),solve(n)用来处理小于n的满足要求的数的个数.枚举素数,判断即可. #include <cstdio> #include <cstring> typedef long long ll; const int

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

UVA 1415 - Gauss Prime(数论,高斯素数拓展)

UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或b为0.推断还有一个数为4 * n + 3形式的素数(用到费马平方和定理) 2.假设a.b都不为0,推断a ^ 2 + b ^ 2 是否为素数 那么这题,提取出sqrt(2)来,就和基本情况一样了. 对于2,变成: 假设a.b都不为0,推断a ^ 2 + 2 b ^ 2是否为素数 对于1.事实上仅仅