Nearly prime numbers - SGU 113(素数)

题目大意:判断一个数是否是两个素数的乘积,如果是,输出Yes,否则No。

分析:先打表求出来一部分素因子,用素数对素数判定还是比较快的。

代码如下:

===============================================================================================================================

#include<stdio.h>
#include<algorithm>
#include<vector>
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;

const int MAXN = 10005;

int p[MAXN]={2};

bool Prime(int n)
{
    if(n == 1 || n==0)
        return false;

    for(int i=0; p[i]*p[i] <= n; i++)
    {
        if(n % p[i] == 0)
            return false;
    }

    return true;
}

int main()
{
    for(int i=3, j=1; i<32000; i++)
    {
        if(Prime(i))
            p[j++] = i;
    }

    int T;

    scanf("%d", &T);

    while(T--)
    {
        int i, N;

        scanf("%d", &N);

        for(i=0; p[i]*p[i] <= N; i++)
        {
            if(N%p[i]==0 && Prime(N/p[i]))
                break;
        }

        if(p[i]*p[i] <= N)
            printf("Yes\n");
        else
            printf("No\n");
    }

    return 0;
}
时间: 2025-01-07 11:19:03

Nearly prime numbers - SGU 113(素数)的相关文章

HDu 2138 How many prime numbers 高效Miller素数测试

题目就是给出一组数,让我们测试其中有多少个是素数. 求素数有测试sqrt(n)个数的方法,有筛子方法,不过对于本题这样的题目来说就都不是高效的. 本题使用Miller Rabin素数测试法,效率奇高,对于不是极其大的整数测试都几乎是常数时间.令人神往的算法啊. 网上有个程序,好像是什么吉林的模板程序,不过我一直没看懂他是什么思路写的,是个AC的程序,不过却是错误的,呵呵,因为程序一直把9当做素数. 于是上网查找了其中原理,自己写了个程序,效率和他的差不多一样,通过时间基本无差别,不过我的思路是按

How many prime numbers(求素数个数)

How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14684    Accepted Submission(s): 5091 Problem Description Give you a lot of positive integers, just to find out how many p

HDU 5108Alexandra and Prime Numbers(大素数)

Problem Description Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime. The problem above is quite easy, so Alexandra gave him a new task: Given

HDu 2138 How many prime numbers 高效Miller素数測试

题目就是给出一组数.让我们測试当中有多少个是素数. 求素数有測试sqrt(n)个数的方法.有筛子方法,只是对于本题这种题目来说就都不是高效的. 本题使用Miller Rabin素数測试法.效率奇高.对于不是极其大的整数測试都差点儿是常数时间.令人神往的算法啊. 网上有个程序,好像是什么吉林的模板程序,只是我一直没看懂他是什么思路写的,是个AC的程序,只是却是错误的,呵呵,由于程序一直把9当做素数. 于是上网查找了当中原理,自己写了个程序,效率和他的几乎相同一样.通过时间基本无区别,只是我的思路是

HDU 2138 How many prime numbers (判素数,米勒拉宾算法)

题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector> #include <cstring> #include <map> #include <cctype> u

HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12955    Accepted Submission(s): 4490 Problem Description Give you a lot of positive integers, just to find out how many pr

hdu5108Alexandra and Prime Numbers(素数的性质)

题目链接: huangjing 思路:每一个数都可以表示成若干个素数的乘积,那么可以对N从2一直枚举到sqrt(N),然后对每个数都能除到不能取余为止,那么后面的合数就不会除了,所以最后得到的数就是最大的质因子,然后直接N/最大的质因子,还有就是N=1的时候没有存在的数  . 题目: Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Accepted: 10619 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations d

[email&#160;protected] Sieve of Eratosthenes (素数筛选算法) &amp; Related Problem (Return two prime numbers )

Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13,