Goldbach`s Conjecture(素筛水题)题解
Goldbach`s Conjecture
Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:
Every even integer, greater than 2, can be expressed as the sum of two primes [1].
Now your task is to check whether this conjecture holds for integers up to 107.
Input
Input starts with an integer T (≤ 300), denoting the number of test cases.
Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).
Output
For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where
1) Both a and b are prime
2) a + b = n
3) a ≤ b
Sample Input
2
6
4
Sample Output
Case 1: 1
Case 2: 1
思路:
水题一只,叫你求n=a+b且a,b都是素数这样的ab的对数。只要素筛一下,就能做了。最好先储存一下10^7以内的素数,数答案时只要到n/2就行了(因为a<=b),不知道不弄会不会超时,可以试一下。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
const int N=10000005; //18
const int MOD=1000;
using namespace std;
bool prime[N];
int p[N/10],pn;
void get_prime(){
pn=0;
memset(prime,false,sizeof(prime));
prime[0]=prime[1]=true;
for(long long i=2;i<N;i++){
if(!prime[i]){
p[pn++]=i;
for(long long j=i*i;j<N;j+=i){
prime[j]=true;
}
}
}
}
int main(){
get_prime();
int T,n,num=1,ans;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
ans=0;
for(int i=0;p[i]<=n/2;i++){
if(prime[n-p[i]]==false) ans++;
}
printf("Case %d: %d\n",num++,ans);
}
return 0;
}
Goldbach`s Conjecture(素筛水题)题解的更多相关文章
- LightOJ1259 Goldbach`s Conjecture —— 素数表
题目链接:https://vjudge.net/problem/LightOJ-1259 1259 - Goldbach`s Conjecture PDF (English) Statistic ...
- World Finals 2017 (水题题解)
看大佬做2017-WF,我这种菜鸡,只能刷刷水题,勉强维持生活. 赛后补补水题. 题目pdf链接,中文的,tls翻译的,链接在这里 个人喜欢在vjudge上面刷题. E Need for Speed ...
- Goldbach's Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想
题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i] i这个数是不是素数 在线性筛后面加个装桶循环即可 #inc ...
- bzoj usaco 金组水题题解(2)
续.....TAT这回不到50题编辑器就崩了.. 这里塞40道吧= = bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 比较经典的最小割?..然而 ...
- bzoj usaco 金组水题题解(2.5)
bzoj 2197: [Usaco2011 Mar]Tree Decoration 树形dp..f[i]表示处理完以i为根的子树的最小时间. 因为一个点上可以挂无数个,所以在点i上挂东西的单位花费就是 ...
- bzoj usaco 金组水题题解(1)
UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...
- Cmd2001的毒瘤水题题解
怕不是我再不写题解这题就该成没人做也没人会的千古谜题了...... T1: 仔细分析题面,发现相同就是广义SAM上节点相同,相似就是广义SAM上为从根到某个点路径的前缀..直接SAM上跑从根开始,每个 ...
- 2006-2007 ACM-ICPC | POJ3380 POJ3384 POJ3385 水题题解
// CF比赛链接:http://codeforces.com/gym/101650 // POJ链接:http://poj.org/searchproblem?field=source&ke ...
- LOJ6303:水题——题解
https://loj.ac/problem/6303 题目来自LOJ. 就记一个公式,设f(n,k)为n!里分解得到的k(k为质数)的个数,则f(n,k)=f(n/k,k)+n/k. 证明很好证,显 ...
随机推荐
- 深入浅出REST(转载)
add by zhj: 参考http://zh.wikipedia.org/zh/REST 需要注意的是,REST是设计风格而不是标准,它也并没有与哪种协议绑定.不过,我们常按REST设计风格来使用H ...
- decltype类型声明- 现代C++新特性总结
decltype类型声明 有时会遇到这样的情况:希望从表达式的类型推断出要定义的变量的类型,但不想用该表达式的值去初始化变量.为了满足这一需求,C++11引入了decltype,它的作用是选择并返回操 ...
- spring用注解简化bean配置
组件扫描: <context:component-scan base-package="com"/> 容器启动后如果发现配置文件有上面的标签会自动扫描对应的包及子包,如 ...
- 搭建私有yum仓库
需要工具: centos7 nginx rsync 新建文件夹存放镜像数据 mkdir /data mkdir /data/yum_data 同步数据 国内开源镜像站没有几个支持rsync,科技大学的 ...
- 机器学习理论基础学习3.5--- Linear classification 线性分类之朴素贝叶斯
一.什么是朴素贝叶斯? (1)思想:朴素贝叶斯假设 条件独立性假设:假设在给定label y的条件下,特征之间是独立的 最简单的概率图模型 解释: (2)重点注意:朴素贝叶斯 拉普拉斯平滑 ...
- 对使用wordpress做开发的童鞋的提醒
[1.]WordPress 4.7.2 以及之前的所有版本都存在以下6个安全问题:记得及时升级啊. 通过媒体文件的元数据的跨网站脚本(XSS)控制字符可以欺骗重定向网址验证管理员可以使用插件删除功能删 ...
- 20154312 曾林 EXP9 Web安全基础
目录 -0.webgoat Could not find source file -1.基础问题回答 -2.环境配置 -3.Injection Flaws ----3.1.Numeric SQL In ...
- 使用 JsPlumb 绘制拓扑图的通用方法
摘要: 实现 JsPlumb 绘制拓扑图的通用方法. 只要服务端返回一个符合指定格式的数据结构,就可以绘制相应的拓扑图. 难度: 中级 示例工程见: http://download.csdn.net ...
- cmd中mysql中文乱码问题
以下为自己亲试: 解决cmd中MySQL查询和命令返回的中文乱码问题 1.修改cmd字符集方法 rem 切换到UTF-8 chcp 65001 rem 切换到默认的GBK chcp 936 rem 美 ...
- python 文件操作 练习:把一个目录下的所有文件名,打印一下,不要包含后缀名
#coding=utf-8 import osos.chdir('d:\\test2')file_list=os.listdir('.')print "file_list:",fi ...