sgu231:Prime Sum(简单数学题)
2015-05-23 17:20
134 查看
题目大意:
~~~~~~求出1⋯n1\cdots n之内所有的质数对,满足它们的和≤n\leq n且和为质数。
分析:
~~~~~~如果是两奇质数相加,和为偶数,肯定不是质数。
~~~~~~所以只能是2+prime2+prime的形式,然后欧拉筛枚举即可。
AC code:
~~~~~~求出1⋯n1\cdots n之内所有的质数对,满足它们的和≤n\leq n且和为质数。
分析:
~~~~~~如果是两奇质数相加,和为偶数,肯定不是质数。
~~~~~~所以只能是2+prime2+prime的形式,然后欧拉筛枚举即可。
AC code:
[code]#include <cstdio> #include <vector> #define pb push_back #define mp make_pair #define ONLINE_JUDGE typedef long long LL; using namespace std; const int MAXN = 1e6+9; int n; int prime[MAXN/5], tot; bool hash[MAXN]; vector< pair<int,int> > ans; void euler(int n) { for(int i = 2; i <= n; ++i) { if(!hash[i]) prime[++tot] = i; for(int j = 1; j <= tot && (LL)i*prime[j] <= n; ++j) { hash[i*prime[j]] = true; if(i%prime[j] == 0) break; } } } int main() { #ifndef ONLINE_JUDGE freopen("sgu231.in", "r", stdin); freopen("sgu231.out", "w", stdout); #endif scanf("%d", &n); euler(n); for(int i = 1; i <= tot && 2+prime[i] <= n; ++i) if(!hash[2+prime[i]]) ans.pb(mp(2, prime[i])); printf("%d\n", ans.size()); for(int i = 0, sz = ans.size(); i < sz; ++i) printf("%d %d\n", ans[i].first, ans[i].second); #ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout); #endif return 0; }
相关文章推荐
- HDU6025-Coprime Sequence-简单数学
- Codeforces 577 B Modulo Sum 简单数学+dp
- hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)
- Sum of Consecutive Prime Numbers(POJ 2739)尺取法+数学问题
- POJ2739:Sum of Consecutive Prime Numbers(简单数论)
- POJ 1844 Sum【简单数学】
- hdoj the Sum of Cube 5053 (简单数学)
- POJ 1844 Sum【简单数学】
- 1210 - Sum of Consecutive Prime Numbers(数学水题)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 C:sum<简单数学>
- 【Smart OJ P2485】Sum of product 简单数学
- hdu 4061 数学(结论简单)
- 经济金融领域简单数学建模和分析:MATLAB成本曲线方程和销售收入直线方程
- lightoj 1148 - Mad Counting 简单数学
- 动画的帧频设定和人眼生理特关系中的简单数学
- [简单DP]Max Sum Plus Plus HDU 102
- 平行四边形数 FZU 2231 简单的计算数学
- hpuoj 【1037】一个简单的数学题【同余定理】
- 洛谷P3769 简单的数学题 莫比乌斯反演+杜教筛
- cf#Wunder Fund Round 2016 -A - Slime Combining-简单数学