素数筛法

hdu 2.1.3

Largest prime factor

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5996 Accepted Submission(s): 1834
Problem Description
Everybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
 
Input
Each line will contain one integer n(0 < n < 1000000).
 
Output
Output the LPF(n).
 
Sample Input
1
2
3
4
5
 
Sample Output
0
1
2
1
3
 
Author
Wiskey
 
Source
HDU 2007-11 Programming Contest_WarmUp
 
Recommend
威士忌
#include<cstdio>
int prim[1000005]= {0};     //全部初始化为0
void init()
{
    int k;
    prim[1]=0;
    k=1;
    for(int i=2; i<1000000; i++)       //素数筛法
        if(prim[i]==0)//i是素数
        {
            for(int j=i; j<1000000; j+=i)
                prim[j]=k;
            k++;
        }
}
int main()
{
    int n;
    init();
    while(~scanf("%d",&n))
        printf("%d\n",prim[n]);
}
素数筛法就是不标记本身,标记本身的倍数。



××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
以下为我碰到的一些素数题

How many prime numbers

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10657 Accepted Submission(s): 3496
 
Problem Description
Give you a lot of positive integers, just to find out how many prime numbers there are.
 
Input
There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.
 
Output

            For each case, print the number of prime numbers you have found out.
 
Sample Input
3
2 3 4
 
Sample Output
2
 
Author
wangye
 
Source
HDU 2007-11 Programming Contest_WarmUp
 
Recommend
威士忌



题意是找出素数的个数(最简单的)
<pre name="code" class="cpp">#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 100000000
int a[N];
int main()
{
    int n;
    while(~scanf("%d", &n))
    {

        for(int i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
        }
        int sum=0;
        for(int i=0; i<n; i++)
        {
            int sign=0;
            for(int j=2; j<=sqrt(a[i]*1.0); j++)
            {
                if(a[i]%j==0)
                {
                    sign=1;
                    break;
                }

            }
            if(sign==0)
                {
                    sum++;
                    //cout<<sum<<"**"<<endl;
                }
        }

        printf("%d\n", sum);
    }
}


 
     

Largest prime factor

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6009 Accepted Submission(s): 1840
 
Problem Description
Everybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
 
Input
Each line will contain one integer n(0 < n < 1000000).
 
Output
Output the LPF(n).
 
Sample Input
1
2
3
4
5
 
Sample Output
0
1
2
1
3
 
Author
Wiskey
 
Source
HDU 2007-11 Programming Contest_WarmUp
 
Recommend
威士忌





题意是找出找出一个数最大素因子在素数表的位置

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 1000000+5
int prime[N]={0};
void init(){
    int k;
    prime[1]=0;
    k=1;
    for(int i=2; i<N; i++){
        if(prime[i]==0){
            for(int j=i; j<N; j+=i){
                prime[j]=k;
            }
            k++;
        }
    }
}
int main(){
    int n;
    init();
    while(~scanf("%d", &n)){
       printf("%d\n", prime[n]);
    }
}

七夕节

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5469 Accepted Submission(s): 1911
 
Problem Description
七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!"
人们纷纷来到告示前,都想知道谁才是自己的另一半.告示如下:



数字N的因子就是所有比N小又能被N整除的所有正整数,如12的因子有1,2,3,4,6.
你想知道你的另一半吗?
 
Input
输入数据的第一行是一个数字T(1<=T<=500000),它表明测试数据的组数.然后是T组测试数据,每组测试数据只有一个数字N(1<=N<=500000).
 
Output
对于每组测试数据,请输出一个代表输入数据N的另一半的编号.
 
Sample Input
3
2
10
20
 
Sample Output
1
8
22
 
Author
Ignatius.L
 
Source
杭电ACM省赛集训队选拔赛之热身赛
 
Recommend
Eddy





题意是求一个数所有因子的和;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 500000+5
int pose[N];
int n;
void init(){
    for(int i=1; i<N; i++){
        pose[i]=1;
    }
    for(int i=2; i<N; i++){
        for(int j=i+i; j<N; j+=i){
            pose[j]+=i;
            //if(j==n)
              //  printf("i:%d j:%d pose:%d\n", i, j, pose[j]);
        }
    }
}
int main(){
init();
    int t;
    scanf("%d", &t);
    while(t--){
        //int n;
        scanf("%d", &n);

        if(n==1)
        {printf("0\n"); continue;}
        printf("%d\n", pose[n]);
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值