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. 证明很好证,显 ...
随机推荐
- Tomcat的overview界面浅析
Server Locations配置有三个选项(加载位置): 1. Use workspace metadata (does not modify Tomcat installation); 2. U ...
- zabbix 自定义监控项 获取nginx监控状态
本篇是基于zabbix 源码安装的 [root@localhost conf]# curl "http://192.168.0.73:8080/nginx_status" Acti ...
- vue学习之node.js
Node.js是一个Javascript运行环境(runtime environment),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.本文详细介绍了No ...
- cookie和session的使用
http://www.cnblogs.com/linguoguo/p/5106618.html 1:在控制器中添加session和cookie @RequestMapping("/getin ...
- python 读取配置文件总是报错 configparser.NoSectionError: No section:
本文为作者原创,禁止转载,违者必究法律责任!!! python 读取配置文件总是报错 configparser.NoSectionError: No section: 无论是 python2的版本,还 ...
- iOS 第三方框架-MBProgressHUD
MBProgressHUD提示框官网地址:https://github.com/jdg/MBProgressHUD 官网里已经提供了足够多的例子供我们使用,但在实现开发中,我们用到的只是其中的一小部分 ...
- Apache+php+mysql环境配置
Apache+PHP+MySQL环境搭建 标题格式 正文格式 阶段性完成格式 正文中强调格式 ————————————————————————————— 前语:本文是从我写过的doc文档迁移过来的,由 ...
- mysql重做日志
一.重做日志(redo log) 1.作用 确保事务的持久性. 防止在发生故障的时间点,尚有脏页未写入磁盘,在重启mysql服务的时候,根据redo log进行重做,从而达到事务的持久性这一特性. 2 ...
- 2:3 Action的配置
< 一 作用> 一:封装工作单元(相当于是控制层,封装出modelAndView) 二:定义name属性接受前台传过来的数据,再定义message属性,用于存放返回前台页面展示的数 据,实 ...
- Intermediate Python for Data Science learning 2 - Histograms
Histograms from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotlib? ...