F - Goldbach`s Conjecture kuangbin 基础数论
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
Hint
- 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, ...
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 10000001
#define L 31
#define INF 1000000009
#define eps 0.00000001
/*
打表 把所有素数存到一个vector中
然后用一个map保存所有和出现的次数
然后直接找就可以
*/
bool notprime[MAXN];
vector<int> prime; void Init()
{
memset(notprime, false, sizeof(notprime));
notprime[] = true;
for (int i = ; i < MAXN; i++)
{
if (!notprime[i])
{
prime.push_back(i);
for (int j = i + i; j < MAXN; j += i)
notprime[j] = true;
}
}
}
int main()
{
Init();
int T,n;
cin >> T;
for(int cas=;cas<=T;cas++)
{
cin >> n;
vector<int>::iterator p = lower_bound(prime.begin(), prime.end(), n/);
//cout << *p << endl;
int cnt = ;
for (vector<int>::iterator it = prime.begin(); it <= p && *it<=n/; it++)
{
if (!notprime[n - *it])
{
//cout << *it << ' ' << n - *it << endl;
cnt++;
}
}
printf("Case %d: %d\n", cas, cnt);
}
return ;
}
F - Goldbach`s Conjecture kuangbin 基础数论的更多相关文章
- F - Goldbach`s Conjecture 对一个大于2的偶数n,找有多少种方法使两个素数的和为n;保证素数a<=b; a+b==n; a,b都为素数。
/** 题目:F - Goldbach`s Conjecture 链接:https://vjudge.net/contest/154246#problem/F 题意:对一个大于2的偶数n,找有多少种方 ...
- 【LightOJ1259】Goldbach`s Conjecture(数论)
[LightOJ1259]Goldbach`s Conjecture(数论) 题面 Vjudge T组询问,每组询问是一个偶数n 验证哥德巴赫猜想 回答n=a+b 且a,b(a<=b)是质数的方 ...
- [暑假集训--数论]poj2262 Goldbach's Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...
- Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】
Goldbach`s Conjecture(LightOJ - 1259)[简单数论][筛法] 标签: 入门讲座题解 数论 题目描述 Goldbach's conjecture is one of t ...
- Goldbach’s Conjecture(信息学奥赛一本通 1622)
[题目描述] 原题来自:Ulm Local,题面详见:POJ 2262 哥德巴赫猜想:任何大于 44 的偶数都可以拆成两个奇素数之和. 比如: 8=3+5 20=3+17=7+13 42=5+37=1 ...
- Goldbach's Conjecture
Goldbach's Conjecture Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- Poj 2262 / OpenJudge 2262 Goldbach's Conjecture
1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)
Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...
随机推荐
- MAC地址 初识
MAC地址 即物理地址/硬件地址 地址长度为48位,6字节. 格式为:00-23-5A-15-99-42 一个网卡对应一个MAC地址(比如笔记本,有线网卡有一个MAC地址,无线网卡也有一个MAC地址) ...
- 9.11NOIP模拟题
NOIP模拟赛 by thmyl 题目名称 superman market Lemon_Soda 可执行文件名 superman market Lemon_Soda 输入文件 superman.i ...
- 题解报告:hdu 2516 取石子游戏(斐波那契博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2516 Problem Description 1堆石子有n个,两人轮流取.先取者第1次可以取任意多个, ...
- Java系列学习(三)-基础语法
1.关键字 特点:全部小写 2.标识符 (1)就是给类,接口,方法等起名字的字符序列 (2)组成规则: A:英文大小写字母 B:数字 C:$和_ (3)注意事项: A:不能以数字开头 B:不能是jav ...
- 11.Layers, Containers and Interfaces
图层.容器和接口 当设计一个Ventuz场景时,某些节点的组合或设计会反复出现.例如在演示中使用的按钮或滑块,在整个过程中的呈现和外观都是一致的,唯一变化的是尺寸.位置和标签. 在设计复杂的演示时,另 ...
- C#入门经典 Chapter4 流程控制
4.1布尔逻辑 布尔比较运算符 == != < > <= >= 处理布尔值的布尔值运算符 ! & | ^(异或) 条件布尔运算符 &&am ...
- 使用Kettle增量抽取MongoDB数据实践
需求: 增量抽取MongoDB数据并加载到MSSQL 由于不能使用关系型数据库的自定义SQL, 所以主要遇到的问题有: 增量时间的查询和参数控制 ETL的批次信息和调用参数的写入 第一个问题的解决如下 ...
- JS——void(0)
a标签中阻止跳转: <a href="javascript:;">跳转</a> <a href="javascript:void(0)&qu ...
- Cesium学习笔记(九):导入3D模型(obj转gltf)
在用cesium的过程中难免需要导入别人做好的3D模型,这时候就需要将这些模型转成gltf格式了 当然,官方也给了我们一个网页版的转换器,但是毕竟是网页版的,效率极其低下,文件还不能太大,所以我们就需 ...
- js案例分析
名字取的高大上,其实只是我平时上网浏览遇到的一些我感觉还不错的小题目,再加上我或者是我在网上找到的一些理解,就保存到这里了. 2019/4/2 最新开了个新坑,是一个javascipt30的一些案例 ...