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(素筛水题)题解的更多相关文章

  1. LightOJ1259 Goldbach`s Conjecture —— 素数表

    题目链接:https://vjudge.net/problem/LightOJ-1259 1259 - Goldbach`s Conjecture    PDF (English) Statistic ...

  2. World Finals 2017 (水题题解)

    看大佬做2017-WF,我这种菜鸡,只能刷刷水题,勉强维持生活. 赛后补补水题. 题目pdf链接,中文的,tls翻译的,链接在这里 个人喜欢在vjudge上面刷题. E Need for Speed ...

  3. Goldbach's Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想

    题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i]  i这个数是不是素数  在线性筛后面加个装桶循环即可 #inc ...

  4. bzoj usaco 金组水题题解(2)

    续.....TAT这回不到50题编辑器就崩了.. 这里塞40道吧= = bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 比较经典的最小割?..然而 ...

  5. bzoj usaco 金组水题题解(2.5)

    bzoj 2197: [Usaco2011 Mar]Tree Decoration 树形dp..f[i]表示处理完以i为根的子树的最小时间. 因为一个点上可以挂无数个,所以在点i上挂东西的单位花费就是 ...

  6. bzoj usaco 金组水题题解(1)

    UPD:我真不是想骗访问量TAT..一开始没注意总长度写着写着网页崩了王仓(其实中午的时候就时常开始卡了= =)....损失了2h(幸好长一点的都单独开了一篇)....吓得赶紧分成两坨....TAT. ...

  7. Cmd2001的毒瘤水题题解

    怕不是我再不写题解这题就该成没人做也没人会的千古谜题了...... T1: 仔细分析题面,发现相同就是广义SAM上节点相同,相似就是广义SAM上为从根到某个点路径的前缀..直接SAM上跑从根开始,每个 ...

  8. 2006-2007 ACM-ICPC | POJ3380 POJ3384 POJ3385 水题题解

    // CF比赛链接:http://codeforces.com/gym/101650 // POJ链接:http://poj.org/searchproblem?field=source&ke ...

  9. LOJ6303:水题——题解

    https://loj.ac/problem/6303 题目来自LOJ. 就记一个公式,设f(n,k)为n!里分解得到的k(k为质数)的个数,则f(n,k)=f(n/k,k)+n/k. 证明很好证,显 ...

随机推荐

  1. (转)Spring实现IoC的多种方式

    原文地址:http://www.cnblogs.com/best/p/5727935.html 目录 一.使用XML配置的方式实现IOC 二.使用Spring注解配置IOC 三.自动装配 四.零配置实 ...

  2. operator, itertools

    import operator import itertools info_list = [ {'name': 'Quinn', 'age': 50}, {'name': 'Ryan', 'age': ...

  3. mongodb基础语法

    Mongodb与关系型数据库最大的区别就是无约束, 既无字段(外键等)约束, 也没有数据类型约束, 以json存储 安装 启动Mongodb(默认在c盘找 data/db/文件夹) 服务端: mong ...

  4. sql优化 表连接join方式

        sql优化核心 是数据库中 解析器+优化器的工作,我觉得主要有以下几个大方面:1>扫表的方法(索引非索引.主键非主键.书签查.索引下推)2>关联表的方法(三种),关键是内存如何利用 ...

  5. 2018最新php笔试题及答案(持续更新)

    php中include和require的区别 在 PHP 中,您可以在服务器执行 PHP 文件之前在该文件中插入一个文件的内容.include 和 require 语句用于在执行流中插入写在其他文件中 ...

  6. 包管理 ----- Linux操作系统rpm包安装方式步骤

    Linux操作系统rpm包安装方式步骤 2016年08月04日 07:00:26 阅读数:17140 转自 : http://os.51cto.com/art/201003/186467.htm 特别 ...

  7. pod 的warning一定要注意消除,要不然你的pod配置有问题无法导入

    and the CHANGELOG for this version http://git.io/BaH8pQ. Downloading dependencies Using BlocksKit (2 ...

  8. python的三种字符串格式化方法

    1.最方便的 print 'hello %s and %s' % ('df', 'another df') 但是,有时候,我们有很多的参数要进行格式化,这个时候,一个一个一一对应就有点麻烦了,于是就有 ...

  9. testng入门教程2用TestNG编写测试及执行测试

    编写TestNG测试基本上包括以下步骤: 测试和编写业务逻辑,在代码中插入TestNG的注解.. 添加一个testng.xml文件或build.xml中在测试信息(例如类名,您想要运行的组,等..) ...

  10. 019-centos的yum用法

    1.检测系统是否已经安装过mysql或其依赖:# yum list installed | grep mysql(当然也可以用 rpm -qa | grep mysql) 2.卸载已经存在的mysql ...