POJ2909_Goldbach's Conjecture(线性欧拉筛)
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.
A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
InputAn integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
OutputEach output line should contain an integer number. No other characters should appear in the output.
Sample Input
6
10
12
0
Sample Output
1
2
1
题意:给你一个大于等于4小于2^15的偶数,求有多少对素数满足n=p1+p2,不应将(p1,p2)和(p2,p1)分别计算为两个不同的对 线性欧拉筛 代码:
import java.util.Scanner;
public class Main {
static final int max=34000;
static int prime[]=new int[max];
static boolean is_prime[]=new boolean[max];
static int k=0;
public static void Prime(){
is_prime[0]=is_prime[1]=true;
for(int i=2;i<max;i++){
if(!is_prime[i]) prime[k++]=i;
for(int j=0;j<k&&prime[j]*i<max;j++){
is_prime[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
public static void main(String[] args) {
Prime();
Scanner scan=new Scanner(System.in);
while(scan.hasNext()){
int n=scan.nextInt();
if(n==0) break;
int cnt=0;
for(int i=0;i<k&&prime[i]<=n/2;i++){
if(!is_prime[n-prime[i]]) cnt++;
}
System.out.println(cnt);
}
}
}
POJ2909_Goldbach's Conjecture(线性欧拉筛)的更多相关文章
- hdu3572线性欧拉筛
用线性筛来筛,复杂度O(n) #include<bits/stdc++.h> #include<ext/rope> #define fi first #define se se ...
- BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)
标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.. 思考:推,. 建立gcd(x,y) = p,然后,x / p与y / p互 ...
- Goldbach's Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想
题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i] i这个数是不是素数 在线性筛后面加个装桶循环即可 #inc ...
- Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛
题意 给出a d n 给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 #include<cstdio> #inclu ...
- Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)
题意:给一个数 可以写出多少种 连续素数的合 思路:直接线性筛 筛素数 暴力找就行 (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net ...
- BZOJ 2190 SDOI 2008 仪仗队 线性欧拉筛
标题效果:有一个格子组件图,假设三个人在一条直线上,那么第一个人将不会看到第三人.现在,有一个人站在(1,1)在.我问他是否能看到n*n的人数的矩阵. 思考:如果你想站(1,1)这名男子看到了一个立场 ...
- The Embarrassed Cryptographer POJ - 2635 同余模+高精度处理 +线性欧拉筛(每n位一起处理)
题意:给出两数乘积K(1e100) 和 一个数L(1e6) 问有没有小于L(不能等于)的素数是K的因数 思路:把数K切割 用1000进制表示 由同余模公式知 k%x=(a*1000%x+b* ...
- HDU 3823 Prime Friend(线性欧拉筛+打表)
Besides the ordinary Boy Friend and Girl Friend, here we define a more academic kind of friend: Prim ...
- 欧拉筛,线性筛,洛谷P2158仪仗队
题目 首先我们先把题目分析一下. emmmm,这应该是一个找规律,应该可以打表,然后我们再分析一下图片,发现如果这个点可以被看到,那它的横坐标和纵坐标应该互质,而互质的条件就是它的横坐标和纵坐标的最大 ...
随机推荐
- C# 索引器的理解和使用
概述 此部分内容引用自MSDN文档 使用索引器可以用类似于数组的方式为对象建立索引. get 取值函数返回值. set 取值函数分配值. this 关键字用于定义索引器. value 关键字用于定义 ...
- Python学习笔记三:数据特征分析
完成数据清理后,下面通过图表展开对数据的分析. 1.前期初判(分布分析): 1)判断分组区间: # a.散点图:plt.scatter(data[字段1],data['字段2'], s = data[ ...
- Jmeter-集合点与关联
1.集合点 添加同步定时器(Synchronizing Timer) 注意:集合点需要放在需要集合的元件前面 2.关联 预先使用Badboy录制WebTours登录脚本 在登录前插入一个监听器:察看结 ...
- 【基础问题】String 类型为什么是final类型
关于为什么此类问题,都能列出12345...种种原因 已创建具体的值不可修改,故本身就是线程安全的,放到多线程中也不会担心被修改,支持在多线程中共享自身 . 因为字符串是不可变的,所以在它创建的时候H ...
- Spring Data JPA介绍与简单案例
一.Spring Data JPA介绍 可以理解为JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL(Java Persistence Query Languag ...
- 回形数字矩阵(Java)
将矩阵从里到外分为多层,每一层都是一个口字型数字序列,方向都是顺时针/逆时针,由此我们可以将问题分解为相同的子问题来解决 回形矩阵概述 ☃ 回形矩阵有n行n列 ☃ 数字按顺时针或者逆时针递增 **使用 ...
- 番外:可刷新PDB的管理操作(如何切换PDB Switching Over)
基于版本:19c (12.2.0.3) AskScuti 主题:可刷新PDB如何进行切换操作 内容说明:本篇延续如何克隆可刷新的PDB(Refreshable PDB)一文,进行切换实验. 具体请参考 ...
- JS高级---浅拷贝
浅拷贝 拷贝就是复制, 就相当于把一个对象中的所有的内容, 复制一份给另一个对象, 直接复制, 或者说, 就是把一个对象的地址给了另一个对象, 他们指向相同, 两个对象之间有共同的属性或者方法, ...
- 松软科技课堂:jQuery 事件函数
jQuery 事件函数 jQuery 事件处理方法是 jQuery 中的核心函数. 事件处理程序指的是当 HTML 中发生某些事件时所调用的方法.术语由事件“触发”(或“激发”)经常会被使用. 通常会 ...
- 复选框与bitmap算法实践
bitmap(位图)算法 bitmap算法是利用数据二进制的每一位的值来表示数据的算法,可用来压缩保存数据集. 如何保存 如 5(int)的二进制表示为 101b,第一位和第三位的值是1就可以表示数据 ...