[數論] HOJ 2561 MaoLaoDa Number 篩素數

傳送門:MaoLaoDa Number

MaoLaoDa Number

My Tags   (Edit)
  Source : MaoLaoDa
  Time limit : 5 sec   Memory limit : 64 M

Submitted : 711, Accepted : 109

Background

MaoLaoDa likes studying numbers, especially the prime numbers, very much. One day, MaoLaoDa found that a number could be expressed as the product of two prime numbers. For instance, 10 = 2 x 5, and he called this kind of numbers MaoLaoDa Numbers. Now, give you a number N and you should tell me whether it is a MaoLaoDa number or not.

Input

Multiple test cases, each contains a positive integer, N (N <= 231 - 1).

Output

For each case, print "Yes" when N is a MaoLaoDa number, or you should print "No".

Sample Input

10
11

Sample Output

Yes
No

解題報告:

此題就是問一個數是否能化爲兩個素數相乘。範圍在int型內,開根號可知只需要找到前50000個數中的素數即可。代碼如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int prime[6000],num;
bool visited[50000];
void isprime(){
    num=0;
    memset(visited,0,sizeof(visited));
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=50000; i++){
        if(visited[i] == 0)
            prime[num++] = i;
        for(int j=0; j<num && prime[j]*i<=50000; j++){
            visited[prime[j]*i] = 1;
            if(i%prime[j] == 0)
                break;
        }
    }
}
bool primes(int n){
    for(int i=0;prime[i]*prime[i]<=n;i++)
        if(n%prime[i]==0)
            return false;
    return true;
}
int main(){
    isprime();
    int n;
    while(scanf("%d",&n)==1){
        bool flag=false;
        for(int i=0;(long long)prime[i]*prime[i]<=n;i++)
            if(n%prime[i]==0)
                if(primes(n/prime[i])){
                    flag=true;
                    break;
                }
        if(flag)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章