POJ3292 Semi-prime H-numbers

定义H数为4n+1,n>=0的数,H素数为H数中只能拆成1*x,x为H数的数,求1-n<=1000000中H素数的个数。

筛法。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<iostream>
 6 using namespace std;
 7
 8 int n;
 9 #define maxn 1000011
10 int prime[maxn],lp=0;bool notp[maxn];
11 void makeprime()
12 {
13     memset(notp,0,sizeof(notp));
14     notp[1]=0;
15     for (int i=5;i<=n;i+=4)
16         if (!notp[i])
17         {
18             prime[++lp]=i;
19             for (int j=i*5;j<=n;j+=(i<<2)) notp[j]=1;
20         }
21 }
22 bool isans[maxn];int ans[maxn];
23 int main()
24 {
25     n=1000001;
26     makeprime();
27     memset(isans,0,sizeof(isans));
28     for (int i=1;i<=lp;i++)
29         for (int j=1;prime[j]*prime[i]<=n;j++)
30             isans[prime[i]*prime[j]]=1;
31     ans[0]=0;
32     for (int i=1;i<=n;i++) ans[i]=ans[i-1]+isans[i];
33     while (scanf("%d",&n) && n)
34         printf("%d %d\n",n,ans[n]);
35     return 0;
36 }

时间: 2024-08-04 10:05:43

POJ3292 Semi-prime H-numbers的相关文章

10001st prime

problem 7:10001st prime 题意:求第10001个质数 代码如下: 1 #ifndef PRO7_H_INCLUDED 2 #define PRO7_H_INCLUDED 3 4 #include "prime.h" 5 6 namespace pro7{ 7 int solve(){ 8 int p[200005]; 9 getPrime(200000,p); 10 return p[10000]; 11 } 12 } 13 14 #endif // PRO7_H

Largest prime factor

problem 3:Largest prime factor 题意:求600851475143的最大的质因数 代码如下: 1 #ifndef PRO3_H_INCLUDED 2 #define PRO3_H_INCLUDED 3 4 #include "prime.h" 5 6 namespace pro3{ 7 long long solve(){ 8 long long n=600851475143LL,maxn=0;; 9 for(long long i=1;i*i<=n;

F - Dima and Lisa

Problem description Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes. More formally, you are gi

CSE 216 – Homework

CSE 216 – Homework IThis homework document consists of 3 pages. Carefully read the entire document before you start coding.Note: All functions, unless otherwise specified, should be polymorphic (i.e., they should work with anydata type). For example,

Summation of primes

problem 10:Summation of primes 题意:求不大于200w的素数和 代码如下: 1 #ifndef PRO10_H_INCLUDED 2 #define PRO10_H_INCLUDED 3 4 #include "prime.h" 5 #include <cstring> 6 7 int p[2000000]; 8 bool vis[10000005]; 9 long long solve(){ 10 memset(vis,0,sizeof(vi

Smallest multiple

problem 5:Smallest multiple 题意:求最小的正数,使得其可以被1-20整除 代码如下: 1 #ifndef PRO5_H_INCLUDED 2 #define PRO5_H_INCLUDED 3 4 #include "prime.h" 5 6 namespace pro5{ 7 long long solve(){ 8 long long ans=1; 9 for(int i=2;i<=20;++i) 10 ans=lcm(ans,i); 11 ret

Codeforces Round #324 (Div. 2) D. Dima and Lisa (哥德巴赫猜想 + 暴力)

D. Dima and Lisa Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes. More formally, you are given

bzoj4373:算数天才与等差数列

算术天才⑨非常喜欢和等差数列玩耍. 有一天,他给了你一个长度为n的序列,其中第i个数为a[i]. 他想考考你,每次他会给出询问l,r,k,问区间[l,r]内的数从小到大排序后能否形成公差为k的等差数列. 当然,他还会不断修改其中的某一项. 为了不被他鄙视,你必须要快速并正确地回答完所有问题. 注意:只有一个数的数列也是等差数列. 第一行包含两个正整数n,m(1<=n,m<=300000),分别表示序列的长度和操作的次数. 第二行包含n个整数,依次表示序列中的每个数a[i](0<=a[i]

CodeForces 584D Dima and Lisa

1e9 以内的判断一个数是否是素数,可以直接朴素的暴力. 这倒题除了考虑1e9以内的素数的判断,还有一个歌德巴赫猜想:任意一个奇数都可一分解为三个素数的和. 第三个结论:素数是密集的,1e9以内,相邻的素数之间的间隔不会大于300,所以直接枚举也不会浪费掉太多的时间. 这里还有一点需要注意的是:朴素的判断是否是素数,i<=saqrt(n).今天的天梯赛由于记错了这个条件,导致没有求出素数,一道二十分的题没有做,好伤心. Dima and Lisa Time Limit:1000MS     Me

JNI线程模式

作者:左少华 博客:http://blog.csdn.net/shaohuazuo/article/details/43149193 转载请注明出处:http://blog.csdn.net/shaohuazuo 1.Android线程介绍. 1.线程的概念: 这个是百度百科里的一段话. 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元. 一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成. 另外,线程是进程中的一个实体,是被