SGU113 水题 Easy Problem
2014-01-28 17:15
281 查看
问题:判断一个数是否是两个质数的乘积。
Problem: Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if
given number is nearly prime and “No” otherwise.
解法:筛法求质数,再枚举。枚举时要用O(Sqrt(N))的复杂度判断另一个质数。
Solution: Obtain prime numbers by sieving method, then enumerate them. Please note that you need to use the method of complexity of O(Sqrt(N)) to judge the other number whether it is a prime number in case it is very large.
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <math.h>
using namespace std;
int n,m,pnum,p[100100];
bool f[1000100];
bool isprime(int x) {
if (x<2) return 0;
for (int i=2;i*i<=x;i++)
if (x%i==0) return 0;
return 1;
}
int main() {
pnum = 0;
memset(f,0,sizeof(f));
f[1] = true;
for (int i=2;i<=1000010;i++)
if (!f[i]) {
pnum++;
p[pnum] = i;
for (int j=i+i;j<=1000010;j+=i) f[j] = true;
}
scanf("%d",&n);
while (n--) {
bool ans = false;
scanf("%d",&m);
for (int i=1;i<=pnum;i++) {
if (p[i]*p[i]>m) break;
if ((m%p[i]==0) && isprime(m/p[i])) {
ans = true;
break;
}
}
if (ans) printf("Yes\n");
else printf("No\n");
}
return 0;
}
Problem: Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if
given number is nearly prime and “No” otherwise.
解法:筛法求质数,再枚举。枚举时要用O(Sqrt(N))的复杂度判断另一个质数。
Solution: Obtain prime numbers by sieving method, then enumerate them. Please note that you need to use the method of complexity of O(Sqrt(N)) to judge the other number whether it is a prime number in case it is very large.
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <math.h>
using namespace std;
int n,m,pnum,p[100100];
bool f[1000100];
bool isprime(int x) {
if (x<2) return 0;
for (int i=2;i*i<=x;i++)
if (x%i==0) return 0;
return 1;
}
int main() {
pnum = 0;
memset(f,0,sizeof(f));
f[1] = true;
for (int i=2;i<=1000010;i++)
if (!f[i]) {
pnum++;
p[pnum] = i;
for (int j=i+i;j<=1000010;j+=i) f[j] = true;
}
scanf("%d",&n);
while (n--) {
bool ans = false;
scanf("%d",&m);
for (int i=1;i<=pnum;i++) {
if (p[i]*p[i]>m) break;
if ((m%p[i]==0) && isprime(m/p[i])) {
ans = true;
break;
}
}
if (ans) printf("Yes\n");
else printf("No\n");
}
return 0;
}
相关文章推荐
- SGU114 水题 Easy Problem
- SGU 水题集合2 SGU112 SGU113 SGU114 SGU115
- UESTC 1300 Easy Problem 水题
- nyoj.811 变态最大值【水题】 20141125
- java-------华为机试-------汽水瓶(水题)
- HDOJ 5122 K.Bro Sorting 水题
- hdu 1029 Ignatius and the Princess IV(水题)
- Codeforces Round #281 (Div. 2) A. Vasya and Football 暴力水题
- CodeForces - 808B Average Sleep Time(水题+规律)
- HDU2029(回文串水题)
- CodeForces 444C. DZY Loves Physics(枚举+水题)
- Treasure - CodeForces 494 A 水题
- Elastic Ball in the Frame(水题)
- hdu 5146 暴力水题
- PAT 1019 General Palindromic Number(进制水题)
- HDU1898 Sempr == The Best Problem Solver?【水题】
- CodeForces-698A Vacations(水题)
- poj 3051 Satellite Photographs dp水题
- 整除(水题)
- hdu 2057 A + B Again (水题)