素数筛 LightOJ
来源:互联网 发布:淘宝网灯具城 编辑:程序博客网 时间:2024/06/06 14:17
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) where1) Both a and b are prime2) a + b = n3) a ≤ b
Sample Input
264
Sample Output
Case 1: 1Case 2: 1
题意就是找一个偶数可以分解为几对素数之和,数据有些大,需要用筛法求素数。
#include <iostream>#include <cmath>#include <cstring>#include <algorithm>#include <stdio.h>using namespace std;typedef long long ll;const int INF=0x3f3f3f3f;const int maxn=1e7;bool vis[maxn];int pri[maxn/10];void prime(){ memset(vis,0,sizeof(vis)); int num=0; for(int i=2;i<maxn;i++) { if(!vis[i]) //原理就是每次标记所有约数有i的数,然后遍历所有没有被标记过的数 { pri[++num]=i; for(int j=i*2;j<=maxn;j+=i) vis[j]=1; } }}int main(){ prime(); int t,n; cin>>t; for(int cas=1;cas<=t;cas++) { cin>>n; int ans=0,tmp=n/2; for(int i=1;pri[i]<=tmp;i++) { if(vis[n-pri[i]]==0) { ans++; } } cout<<"Case "<<cas<<": "<<ans<<endl; } return 0;}
阅读全文
0 0
- 素数筛 LightOJ
- lightoj 1259 - Goldbach`s Conjecture 【素数筛】
- LightOJ - 1197 Help Hanzo 素数筛
- LightOJ 1370 Bi-shoe and Phi-shoe(素数筛)
- LightOJ 1197 Help Hanzo(区间素数筛)
- LightOj 1259 Goldbach`s Conjecture 素数筛法
- LightOJ 1197(大数范围筛区间内素数个数)
- LightOj 1341 Aladdin and the Flying Carpet (素数筛+求一个数的约数个数)
- LightOJ 1356 Prime Independence (素数 二分图)
- LightOj 1370(素数筛选打表)
- LightOJ 1356 Prime Independence (素数+二分图)
- lightoj-1289(数论+素数打表)
- Help Hanzo(区间求素数) LightOJ
- LightOJ 1289 LCM from 1 to n (节省空间的素数筛法+n个数的最小公倍数)
- lightOJ 1197 Help Hanzo 两阶段素数筛选
- LightOJ 1197Help Hanzo (区间素数筛选法)
- lightOJ 1197 Help Hanzo (区间找素数)
- LightOj 1259-Goldbach`s Conjecture(素数打表)
- 51nod1799 二分答案(分块打表)
- nodejs中需了解的http协议
- 面向对象的内存机制分析-1
- UltraEdit实现“删除包含某个关键字的所有行”
- 8.OD--函数参考
- 素数筛 LightOJ
- Python学习随笔六:抽象之面向对象
- Android -对图片Drawable进行变色
- 9.OD-断点、命令
- HDOJ1208 记忆化搜索水题
- Python笔记之模块版本号
- area 估算函数(simpson)
- 8月21日训练
- 字节输入流 read 读取文件