UVA583 UVALive5406 Prime Factors【素数因子+筛选法】
2018-02-20 08:59
609 查看
Webster defines prime as:
prime (pr¯im) n. [ME, fr. MF, fem. of prin first, L primus; akin to L prior] 1: first in time: original 2 a: having no factor except itself and one ⟨3 is a ∼ number⟩ b : having no common factor except one ⟨12 and 25 are relatively ∼⟩ 3 a: first in rank, authority or significance: principal b: having the highest quality or value ⟨∼ television time ⟩ [from Webster’s New Collegiate Dictionary]
The most relevant definition for this problem is 2a: An integer g > 1 is said to be prime if and only if its only positive divisors are itself and one (otherwise it is said to be composite). For example, the number 21 is composite; the number 23 is prime. Note that the decompositon of a positive number g into its prime factors, i.e.,g = f1 × f2 × · · · × fn
is unique if we assert that fi > 1 for all i and fi ≤ fj for i < j.
One interesting class of prime numbers are the so-called Mersenne primes which are of the form 2^p − 1. Euler proved that 2^31 − 1 is prime in 1772 — all without the aid of a computer.
Input
The input will consist of a sequence of numbers. Each line of input will contain one number g in the range −2^31 < g < 2^31, but different of -1 and 1. The end of input will be indicated by an input line having a value of zero.
Output
For each line of input, your program should print a line of output consisting of the input number and its prime factors. For an input number g > 0, g = f1 × f2 × · · · × fn, where each fiis a prime number greater than unity (with fi ≤ fj for i < j), the format of the output line should be
g = f1 x f2 x . . . x fn
When g < 0, if | g |= f1 × f2 × · · · × fn, the format of the output line should be
g = -1 x f1 x f2 x . . . x fn
Sample Input
-190
-191
-192
-193
-194
195
196
197
198
199
200
0
Sample Output
-190 = -1 x 2 x 5 x 19
-191 = -1 x 191
-192 = -1 x 2 x 2 x 2 x 2 x 2 x 2 x 3
-193 = -1 x 193
-194 = -1 x 2 x 97
195 = 3 x 5 x 13
196 = 2 x 2 x 7 x 7
197 = 197
198 = 2 x 3 x 3 x 11
199 = 199
200 = 2 x 2 x 2 x 5 x 5
Regionals 1997 >> North America - East Central NA
问题链接:UVA583 UVALive5406 Prime Factors
问题简述:(略)
问题分析:
这个问题是分解一个数的素因子。
使用筛选法打表,找出必要的素数备用,计算时间上是节省的。
程序说明:(略)
题记:(略)
参考链接:(略)
AC的C++语言程序如下:/* UVA583 UVALive5406 Prime Factors */
#include <bits/stdc++.h>
using namespace std;
const int N31 = 2147483647; // 2 ^ 31
const int N = ceil(sqrt((double) N31));
const int SQRTN = ceil(sqrt((double) N));
bool isPrime[N + 1];
int prime[N + 1], pcount;
// Eratosthenes筛选法
void esieve(void)
{
memset(isPrime, true, sizeof(isPrime));
isPrime[0] = isPrime[1] = false;
pcount = 0;
for(int i=2; i<=N; i++) {
if(isPrime[i]) {
prime[pcount++] = i;
for(int j=i*i; j<=N; j+=i) //筛选
isPrime[j] = false;
}
}
}
int main()
{
esieve();
int n;
while(~scanf("%d", &n) && n) {
bool flag = true;
printf("%d =", n);
if(n < 0) {
printf(" -1");
n = -n;
flag = false;
}
for(int i=0; i<pcount; i++) {
while(n % prime[i] == 0 && n != 1) {
if(flag) {
printf(" %d", prime[i]);
flag = false;
} else
printf(" x %d", prime[i]);
n /= prime[i];
}
}
if(n != 1) {
if(flag)
printf(" %d", n);
else
printf(" x %d", n);
}
printf("\n");
}
return 0;
}
prime (pr¯im) n. [ME, fr. MF, fem. of prin first, L primus; akin to L prior] 1: first in time: original 2 a: having no factor except itself and one ⟨3 is a ∼ number⟩ b : having no common factor except one ⟨12 and 25 are relatively ∼⟩ 3 a: first in rank, authority or significance: principal b: having the highest quality or value ⟨∼ television time ⟩ [from Webster’s New Collegiate Dictionary]
The most relevant definition for this problem is 2a: An integer g > 1 is said to be prime if and only if its only positive divisors are itself and one (otherwise it is said to be composite). For example, the number 21 is composite; the number 23 is prime. Note that the decompositon of a positive number g into its prime factors, i.e.,g = f1 × f2 × · · · × fn
is unique if we assert that fi > 1 for all i and fi ≤ fj for i < j.
One interesting class of prime numbers are the so-called Mersenne primes which are of the form 2^p − 1. Euler proved that 2^31 − 1 is prime in 1772 — all without the aid of a computer.
Input
The input will consist of a sequence of numbers. Each line of input will contain one number g in the range −2^31 < g < 2^31, but different of -1 and 1. The end of input will be indicated by an input line having a value of zero.
Output
For each line of input, your program should print a line of output consisting of the input number and its prime factors. For an input number g > 0, g = f1 × f2 × · · · × fn, where each fiis a prime number greater than unity (with fi ≤ fj for i < j), the format of the output line should be
g = f1 x f2 x . . . x fn
When g < 0, if | g |= f1 × f2 × · · · × fn, the format of the output line should be
g = -1 x f1 x f2 x . . . x fn
Sample Input
-190
-191
-192
-193
-194
195
196
197
198
199
200
0
Sample Output
-190 = -1 x 2 x 5 x 19
-191 = -1 x 191
-192 = -1 x 2 x 2 x 2 x 2 x 2 x 2 x 3
-193 = -1 x 193
-194 = -1 x 2 x 97
195 = 3 x 5 x 13
196 = 2 x 2 x 7 x 7
197 = 197
198 = 2 x 3 x 3 x 11
199 = 199
200 = 2 x 2 x 2 x 5 x 5
Regionals 1997 >> North America - East Central NA
问题链接:UVA583 UVALive5406 Prime Factors
问题简述:(略)
问题分析:
这个问题是分解一个数的素因子。
使用筛选法打表,找出必要的素数备用,计算时间上是节省的。
程序说明:(略)
题记:(略)
参考链接:(略)
AC的C++语言程序如下:/* UVA583 UVALive5406 Prime Factors */
#include <bits/stdc++.h>
using namespace std;
const int N31 = 2147483647; // 2 ^ 31
const int N = ceil(sqrt((double) N31));
const int SQRTN = ceil(sqrt((double) N));
bool isPrime[N + 1];
int prime[N + 1], pcount;
// Eratosthenes筛选法
void esieve(void)
{
memset(isPrime, true, sizeof(isPrime));
isPrime[0] = isPrime[1] = false;
pcount = 0;
for(int i=2; i<=N; i++) {
if(isPrime[i]) {
prime[pcount++] = i;
for(int j=i*i; j<=N; j+=i) //筛选
isPrime[j] = false;
}
}
}
int main()
{
esieve();
int n;
while(~scanf("%d", &n) && n) {
bool flag = true;
printf("%d =", n);
if(n < 0) {
printf(" -1");
n = -n;
flag = false;
}
for(int i=0; i<pcount; i++) {
while(n % prime[i] == 0 && n != 1) {
if(flag) {
printf(" %d", prime[i]);
flag = false;
} else
printf(" x %d", prime[i]);
n /= prime[i];
}
}
if(n != 1) {
if(flag)
printf(" %d", n);
else
printf(" x %d", n);
}
printf("\n");
}
return 0;
}
相关文章推荐
- UVALive3399 UVA1210 POJ2739 Sum of Consecutive Prime Numbers【素数筛选+尺取法】
- UVALive6050 Primes【素数筛选+前缀和】
- UVA10699 Count the factors【素数因子个数+筛选法】
- uva 10140 素数筛选(两次)
- UVA - 1404 Prime k-tuple (素数筛选)
- HDU - 1999 不可摸数 (所有的真因子和,类似于素数筛选的那个过程写的)
- uva583 Prime Factors
- UVALive 6657 GCD XOR 异或,因子筛法
- 素数筛选,整数的唯一分解,整数所有因子之和,递归求等比数列前n项和
- UVa 11752 (素数筛选 快速幂) The Super Powers
- UVA 583 素数打表(线性)
- ny520 最大素因子 筛选法求素数
- UVa 1644 - Prime Gap(筛选求素数)
- UVA - 11752 The Super Powers 【预处理+素数筛选】
- PAT-1059 Prime Factors (素数因子)
- 数论——素数筛选法与整数的素因子分解
- 数论--素数筛选法与整数的素因子分解
- UVALive2203 UVa10042 Smith Numbers【质因数分解+素数判定+数位之和】
- 素数筛选法 O(n) O(nlgn) uva10357 选择与除法
- UVA 10539 Almost Prime Numbers( 素数因子)