SGU 231.Prime Sum

题意:

  求有多少对质数(a,b)满足a<=b 且a+b也为质数。(a+b<=10^6)



Solution:

  除了2之外的质数都是奇数,两个奇数的和是偶数,不可能是质数。所以题目就是求差为2的质数对的个数。

先用筛法刷出10^6内的质数,用bool数组标记(int型数组会超内存),然后扫一遍筛出来的质数,统计满足要求的答案就行了。

#include <iostream>
using namespace std;
const int INF = 1000002;
bool f[INF];
int ok[80000],out[10000];
int n, sum, ans;
void sift (int n) {
    for (int i = 2; i <= n; i++) {
        if (!f[i]) {
            ok[++sum] = i;
            for (int j = i + i; j <= n; j += i)    f[j] = 1;
        }
    }
}
int main() {
    cin >> n;
    sift (n);
    for (int i = 1; i < sum; i++)
        if (!f[ok[i] + 2])        out[++ans]=ok[i];

    cout << ans << endl;
    for(int i=1;i<=ans;i++)
        cout<<2<<‘ ‘<<out[i]<<endl;
}

Code

时间: 2024-10-25 05:52:10

SGU 231.Prime Sum的相关文章

SGU 231 Prime Sum 求&lt;=n内有多少对素数(a,b)使得a+b也为素数 规律题

题目链接:点击打开链接 题意: 求<=n内有多少对素数(a,b)使得a+b也为素数 思路: 我们发现所有素数间隔都是>=2的,且除了2都是奇数,那么: 奇数+奇数 = 偶数. 所以只有一种情况2+素数=素数. 所以打个素数表,看一下有多少个素数和前面那个素数间隔是2的. #include <stdio.h> #include <string.h> #include <iostream> #include <math.h> #include <

SGU - 123 - The sum (简单数学!)

SGU - 123 The sum Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u Submit Status Description Here is your second problem, keep calm and solve it . Nacci sequence of numbers is known to all : F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1,

Project Euler:Problem 50 Consecutive prime sum

The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds t

SGU[123] The sum

Description 描述 The Fibonacci sequence of numbers is known: F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1, for n>1. You have to find S - the sum of the first K Fibonacci numbers. 斐波那契数列广为大家所知:F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1(其中n > 1).你需要求斐波那契数列前K个数的和S. Input 输入 F

SGU题目总结

SGU还是个不错的题库...但是貌似水题也挺多的..有些题想出解法但是不想写代码, 就写在这里吧...不排除是我想简单想错了, 假如哪位神犇哪天发现请告诉我.. 231.Prime Sum. Find all pairs of prime numbers (A, B) such that A<=B and their sum is also a prime number and does not exceed N.1<=N<=10^6看起来挺难的样子..a prime应该是奇数(除了2)

hoj 1004 Prime Palindromes(还是不够完美)

The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b

POJ_3421_X-factor Chains(素数筛法)

X-factor Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5659   Accepted: 1786 Description Given a positive integer X, an X-factor chain of length m is a sequence of integers, 1 = X0, X1, X2, -, Xm = X satisfying Xi < Xi+1 and Xi

C# 实例练习(第二天)

实例练习 1. 完成简单登录效果,设置用户登录账号密码,清空控制台,进入登录页面,请求用户输入账号.密码和验证码(随机产生),并判断用户输入的信息,给出相应的提示. C#代码如下: 主要知识点: (1)//清空控制台 Console.Clear(); (2)//设置随机数验证码 Random rand = new Random(); int num = rand.Next(1000,10000); 1 //账户设置界面 2 /* 3 Console.WriteLine("----------账户

网页特殊符号HTML代码大全

HTML特殊字符编码大全:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全.                         ′ ´ ? © > > μ µ ? ® & & ° ° ? ¡     ? » | ¦ ÷ ÷ ? ¿ ? ¬ § § ? • ? ½ ? « ? ¶ ¨ ¨ ? ¸ ? ¼ < < ± ± × × ¢ ¢ ? ¾ ˉ ¯ " " ?