[数论] 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值