Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】
Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】
标签: 入门讲座题解 数论
题目描述
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
Note
1. An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...
题意
哥德巴赫猜想:任意大于等于2的偶数,可以拆分成两个质数加和的形式。(该猜想真伪至今未得完整证明。)
给你一个\(n\),问\(n\)按照哥德巴赫猜想有几种拆分方法?\((n \leq 1e7)\)
解析
哥德巴赫猜想虽然无法证明,但在较小的数据范围内,我们是可以通过计算机求得解的。
我们先通过线性筛找到\(1e7\)内的全体质数,不超过\(67e4\)个.这样我们对于每个\(n\),只需要循环一遍质数,看看\(n\)与这个质数的差是否还是质数就可以了。这个差如果是质数,那么就多一种方案.
因为我们存有\(vis[1 \cdots n]\)数组,所以可以\(O(1)\)的判断所求差是不是一个质数.
注意:本题可以使用欧拉筛也可以使用埃氏筛(我看vjudge上的提交代码有埃氏筛的)。
考察内容:素数筛法
通过代码
/*
Problem
LightOJ - 1259
Status
Accepted
Time
236ms
Memory
14468kB
Length
798
Lang
C++
Submitted
2019-11-24 22:23:13
Shared
RemoteRunId
1640384*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e7 + 50;
int prime[670000], cnt = 0; //注意这题有内存限制,prime数组开到1e7会RE(MLE).我提前打表看了一下,1e7以内的质数不超过67e4个.
bool vis[MAXN];
void init() //欧拉筛。线性时间复杂度内筛出质数来.prime数组存质数,vis[i]数组判断i是否为质数.
{
vis[1] = 1;
for(int i = 2; i <= int(1e7 + 5); i ++){
if(!vis[i])
prime[++ cnt] = i;
for(int j = 1; j <= cnt && i * prime[j] <= int(1e7 + 5); j ++){
vis[i * prime[j]] = 1;
if(i % prime[j] == 0)
break;
}
}
return ;
}
int main()
{
init();
int T, n, times = 0;
scanf("%d", &T);
while(T --){
scanf("%d", &n);
int ans = 0;
for(int i = 1; i <= cnt && prime[i] <= n / 2; i ++){ //只需循环到n/2即可.在(n/2, n]区间中出现的序偶<a, b>,在[2, n/2]区间也一定出现过.
if(!vis[n - prime[i]]){
ans ++;
}
}
printf("Case %d: %d\n", ++ times, ans);
}
return 0;
}
Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】的更多相关文章
- Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)
题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质 ...
- LightOJ - 1259 - Goldbach`s Conjecture(整数分解定理)
链接: https://vjudge.net/problem/LightOJ-1259 题意: Goldbach's conjecture is one of the oldest unsolved ...
- 【LightOJ1259】Goldbach`s Conjecture(数论)
[LightOJ1259]Goldbach`s Conjecture(数论) 题面 Vjudge T组询问,每组询问是一个偶数n 验证哥德巴赫猜想 回答n=a+b 且a,b(a<=b)是质数的方 ...
- Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)
Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...
- Help Hanzo (LightOJ - 1197) 【简单数论】【筛区间质数】
Help Hanzo (LightOJ - 1197) [简单数论][筛区间质数] 标签: 入门讲座题解 数论 题目描述 Amakusa, the evil spiritual leader has ...
- Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】
Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...
- Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】
Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...
- [暑假集训--数论]poj2262 Goldbach's Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...
- F - Goldbach`s Conjecture kuangbin 基础数论
Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathemat ...
随机推荐
- 深入浅出 PHP SPL(PHP 标准库)(转)
一.什么是spl库? SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 此扩展只能在php 5.0以后使用,从PHP 5.3.0 不再被关闭,会一直有效.成为php ...
- Flask 教程 第十二章:日期和时间
本文翻译自The Flask Mega-Tutorial Part XII: Dates and Times 这是Flask Mega-Tutorial系列的第十二部分,我将告诉你如何以适配所有用户的 ...
- Ligg.EasyWinApp-100-Ligg.EasyWinForm:一款Winform应用编程框架和UI库介绍
本项目是一个Winform应用编程框架和UI库.通过这个该框架,不需任何代码,通过XML配置文件,搭建任意复杂的Windows应用界面,以类似Execel公式的方式实现基本的过程控制(赋值.条 ...
- Android Fragment 多层叠加时点击穿透解决方案
一.问题现象 多层fragment叠加时,点击上层fragment会使下层fragment的控件对应点击事件响应,这种现象就是点击穿透. 对于这种情况,我们一般都是对baseFragment进行vie ...
- Vi/Vim常用命令(附快捷切换方法)
vi/vim有两种模式,正常(命令行)模式 和编辑模式,在命令行模式下,任何键盘输入都是命令,在编辑模式下,键盘输入的才是字符. 启动/关闭Vi/Vim 启动:vi 打开 Vi/Vim编辑器vi 文件 ...
- MATLAB实例:绘制折线图
MATLAB实例:绘制折线图 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 条形图的绘制见:MATLAB实例:绘制条形图 用MATLAB将几组不同的数 ...
- Pipe——高性能IO(三)
Pipelines可以替换掉那些丑陋的封装(kludge).变通(workaround)或妥协(compromise)——用一个在框架中设计优雅的专门的解决方案. 敢肯定,下面所覆盖的那些痛点,对于那 ...
- Python中的模块引用机制
一.模块引用 Def: 在Python 程序中使用另一个文件定义的类(方法).函数.数据等 被引用模块位置.通常 Python2 : "/Library/Python/2.7/site-pa ...
- Redis入门(二)-Redis能够做什么
引言 在上篇文章中,我们讲述了Redis的基本知识让读者对Redis有了基本的了解.那么这一节我们就来看一下Redis究竟能做什么. 上一节我们提到了Redis可用作数据库,高速缓存和消息队列代理.这 ...
- 关于C# webapi ,接口返回字符串和json格式 ,返回值中有反斜杠
最近遇到一个比较郁闷的问题,记录一下 写了一个接口,想返回json 数据,但是返回值中总是带有反斜杠... ,下面来看原因 首先,配置 webapi的路由 App_Start 文件夹下 ,WebApi ...