素数筛选2710

#include<iostream>
using namespace std;
int a[20000],label[5000];
//素数筛选
int main(){
    int n,max,j,t;
    while(cin>>n){
        max=-1;
        for(int i=0;i<n;i++){
            cin>>label[i];
            if(label[i]>max)
                max=label[i];
        }
            for(int i=1;i<=max;i++)
                a[i]=1;
            for(int i=2;i<=max;i++){
                if(a[i]==1){
                    j=i;
                while(j<=max){
                    a[j]=i;
                    j+=i;
                }
                }
            }
            max=-1;
            for(int i=0;i<n;i++){
                if(a[label[i]]>max){
                    max=a[label[i]];
                    t=label[i];
                }
            }
            cout<<t<<endl;
        }
        return 0;
}

时间: 2024-08-06 08:17:13

素数筛选2710的相关文章

hdu 5407 CRB and Candies(素数筛选法,除法取模(乘法逆元))

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5407 解题思路: 官方题解: The problem is just to calculate g(N) =\ LCM(C(N,0), C(N,1), ..., C(N, N))g(N) = LCM(C(N,0),C(N,1),...,C(N,N)). Introducing function f(n) =\ LCM(1, 2, ..., n)f(n) = LCM(1,2,...,n), the

O(N)的素数筛选法和欧拉函数

首先,在谈到素数筛选法时,先涉及几个小知识点. 1.一个数是否为质数的判定. 质数,只有1和其本身才是其约数,所以我们判定一个数是否为质数,只需要判定2~(N - 1)中是否存在其约数即可,此种方法的时间复杂度为O(N),随着N的增加,效率依然很慢.这里有个O()的方法:对于一个合数,其必用一个约数(除1外)小于等于其平方根(可用反证法证明),所以我们只需要判断2-之间的数即可. bool is_prime(int num) { const int border = sqrt(num); for

[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,

HDU 2161 Primes (素数筛选法)

题意:输入一个数判断是不是素数,并规定2不是素数. 析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了... 不说了,直接上代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; typedef long long LL; const int maxn = 16000 + 10; int p

LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1341 Description It's said that Aladdin had to solve seven

Light OJ 1197 1197 - Help Hanzo(大区间素数筛选)

Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he i

素数筛选(模板)

#include <stdio.h> int main() { int i,j,a[505]={0}; for(i=1;i<=500;i++) a[i]=1; for(i=2;i<=500;i++) if(a[i]) for(j=i+i;j<=500;j+=i) a[j]=0; for(i=2;i<=500;i++) if(a[i]) printf("%d ",i); printf("\n"); return 0; } 素数筛选(

素数筛选实现

一般的素数筛选的思路是从2开始,将所有2的倍数去掉,然后从3开始,将3的倍数去掉,然后从下一个素数x开始,将x的倍数去掉...,这样可以将所有素数的倍数去掉.实现代码如下: 1 int PrimeOld() 2 { 3 int i; 4 5 cnt = 0; 6 memset(prime, true, sizeof(prime)); 7 for (i = 2; i < MAX; i++) 8 { 9 if (prime[i]) 10 { 11 primeUse[cnt++] = i; 12 fo

关于素数的快速查找——素数筛选法

利用素数筛选法进行素数的快速查找.原理很简单,素数一定是奇数,素数的倍数一定不是素数.思路如下: 预定义N表示10000,即表示查找10000以内的素数,首先定义数组prime[]对N以内的数进行标记,奇数存为1,偶数存为0,最终实现结果为素数的prime值为1,因此将prime[2]赋值为1(2是素数).之后利用for循环,对N以内的奇数进行遍历(注意for循环的条件控制),for里用if判断是否为素数(奇数),若是,执行内部嵌套的for循环判断该奇数是否为素数,若是则标记为1,若不是则pri