1397

news/2025/2/9 12:13:44/

Goldbach's Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4667    Accepted Submission(s): 1754
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1397

Problem Description
Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.

Input
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.

Output
Each output line should contain an integer number. No other characters should appear in the output.

Sample Input
  
6 10 12 0

Sample Output
  
1 2 1
解题思路:
题目大意就是给你一个n,把n拆成两个素数和形式。问有几个这样的素数集合。
先打个素数表,然后开始遍历素数表,为防止集合发生重复(例如(a,b)、(b,a)这种情况),所以
循环到n/2(包含n/2),当其中一个素数确定后,只要判断另一个数是不是素数就好了·····这时候可以
直接使用打表时用到的ispri数组来直接判断(不然用朴素判断会超时)·····
完整代码:
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
const int maxn = 100001;
bool ispri[maxn];
int pri[maxn];
int t;void dopri()
{memset(ispri , true , sizeof(ispri));ispri[0] = ispri[1] = false;t = 0;for(int i = 2; i <= maxn ; i ++){if(ispri[i]){pri[t ++] = i;for(int j = i * 2 ; j <= maxn ; j += i)ispri[j] = false;}}
}int main()
{#ifdef DoubleQfreopen("in.txt","r",stdin);#endifint n;dopri();while(~scanf("%d",&n) && n){int cnt = 0;for(int i = 0 ; pri[i] <= n / 2 ; i ++){int  k = n - pri[i];if(ispri[k])cnt ++;}printf("%d\n",cnt);}
}



http://www.ppmy.cn/news/576645.html

相关文章

hdu 1390 Binary Numbers

题目地址&#xff1a; http://acm.hdu.edu.cn/showproblem.php?pid1390 题目描述&#xff1a; Binary Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2834 Accepted Submission(s): 1758 Probl…

【路径规划】基于matlab Hybrid A_Star算法机器人路径规划【含Matlab源码 1390期】

⛄一、A_star算法简介 1 A Star算法及其应用现状 进行搜索任务时提取的有助于简化搜索过程的信息被称为启发信息.启发信息经过文字提炼和公式化后转变为启发函数.启发函数可以表示自起始顶点至目标顶点间的估算距离, 也可以表示自起始顶点至目标顶点间的估算时间等.描述不同的…

BZOJ 1390 [CEOI2008] Fence 题解

纪念一下&#xff0c;调了整整两周。 本题题意非常清晰&#xff0c;故不提供题意简述。 Solution 注意到 20 4 < 111 20\times 4<111 204<111&#xff0c;所以即使花 4 4 4 个点包住一棵树也是值的。所以我们考虑包住最多的树&#xff0c;这样一定是最优的。 所…

General error: 1390 Prepared statement contains too many placeholders

欢迎加入PHP技术交流QQ群 370648191、201923866 今天遇到mysql占位符的问题。 问题背景是: 在做一个自己用的股票分析系统的时候&#xff0c;采集单只股票数据&#xff0c;可指定采集时间区间。采集完成后&#xff0c;一次性插入数据库。 题外话开始 (一些经验欠缺的&#xf…

POJ 1390:Blocks

问题描述 你们中的一些人可能玩过一个名为“块”的游戏。一行有n个块&#xff0c;每个框都有一个颜色。这是一个例子&#xff1a;金&#xff0c;银&#xff0c;银&#xff0c;银&#xff0c;银&#xff0c;青铜&#xff0c;青铜&#xff0c;青铜&#xff0c;金。相应的图片将如…

LeetCode 1390. 四因数

1. 题目 给你一个整数数组 nums&#xff0c;请你返回该数组中恰有四个因数的这些整数的各因数之和。 如果数组中不存在满足题意的整数&#xff0c;则返回 0 。 示例&#xff1a; 输入&#xff1a;nums [21,4,7] 输出&#xff1a;32 解释&#xff1a; 21 有 4 个因数&#x…

xtu oj 1390 字母计数

字母计数 题目描述 为了压缩一个只含小写英文字母的字符串&#xff0c;我们使用下面的方式表示它 任一字母c是表达式 任一字母c后接一个整数n也是一个表达式&#xff0c;表示把字母c重复n次&#xff0c;n是一个没有前导零的10进制整数&#xff0c;且 n≥2。 如果s1,s2是表达…

xtu 1390 字母计数 oj

字母计数 题目描述 为了压缩一个只含小写英文字母的字符串&#xff0c;我们使用下面的方式表示它 任一字母c是表达式任一字母c后接一个整数n也是一个表达式&#xff0c;表示把字母c重复n次&#xff0c;n是一个没有前导零的10进制整数&#xff0c;且 n≥2。如果s1,s2是表达式&am…