Noldbach problem
Nick is interested in prime numbers. Once he read about Goldbach problem. It states that every even integer greater than 2 can be expressed as the sum of two primes. That got Nick's attention and he decided to invent a problem of his own and call it Noldbach problem. Since Nick is interested only in prime numbers, Noldbach problem states that at least k prime numbers from 2 to n inclusively can be expressed as the sum of three integer numbers: two neighboring prime numbers and 1. For example, 19 = 7 + 11 + 1, or 13 = 5+ 7 + 1.
Two prime numbers are called neighboring if there are no other prime numbers between them.
You are to help Nick, and find out if he is right or wrong.
The first line of the input contains two integers n (2 ≤ n ≤ 1000) and k (0 ≤ k ≤ 1000).
Output YES if at least k prime numbers from 2 to n inclusively can be expressed as it was described above. Otherwise output NO.
| input |
| 27 2 |
| output |
| YES |
| input |
| 45 7 |
| output |
| NO |
#include <iostream>
#include <cstdio>
#include <cmath> int isPrime(int n); //判断n是否是素数
void initPrime(int n); //将n以内的素数存到数组内 此处用1000即可 using namespace std;
int p[]; //存储2~1000之间的素数
int main()
{
int n, k, i, j, counter=;
cin >> n >> k;
initPrime();
for(i=; i<=n; i++) {
if(isPrime(i)==)
continue;
for(j=; p[j]<i; j++) {
if(p[j]+p[j+]+ == i){
counter++;
break;
}
}
}
if(counter < k)
cout << "NO" <<endl;
else
cout << "YES" <<endl;
return ;
} void initPrime(int n) {
int i, j = ;
for(i=; i<n; i++) {
if(isPrime(i)){
p[j++] = i;
}
}
} int isPrime(int n) {
int i;
for(i=; i<=(int)sqrt(n); i++) {
if(n%i == ) {
return ;
}
}
return ;
}
Noldbach problem的更多相关文章
- Codeforces Beta Round #17 A - Noldbach problem 暴力
A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...
- cf17A Noldbach problem(额,,,素数,,,)
题意: 判断从[2,N]中是否有超过[包括]K个数满足:等于一加两个相邻的素数. 思路: 枚举. 也可以:筛完素数,枚举素数,直到相邻素数和超过N.统计个数 代码: int n,k; int prim ...
- CF17A Noldbach problem 题解
Content 若一个素数可以用比它小的相邻的两个素数的和加 \(1\) 表示,那么称这个素数为"好素数". 给定两个正整数 \(n,k\),问从 \(2\) 到 \(n\) 的好 ...
- Codeforces Beta Round #17 A.素数相关
A. Noldbach problem Nick is interested in prime numbers. Once he read about Goldbach problem. It sta ...
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
随机推荐
- weblogic上部署应用程序
weblogic上部署应用程序有三种方法: 一:修改配置文件config.xml在文件中加入如下代码片段: <app-deployment> <name>FAB</nam ...
- JSON之三:获取JSON文本并解释(以google的天气API为例)
google提供了天气的api,以广州天气为例,地址为: http://api.openweathermap.org/data/2.5/weather?q=guangzhou 返回的结果为: { ...
- table中td,th不能设置margin
首先,我们需要知道的是:我们可以对表格table设置margin,而不能设置padding;对单元格td设置padding,而不能设置margin.所以说,我们不能对单元格td设置margin属性来调 ...
- Android 开发使用lambda实现< JDK8兼容
代码精简无疑是每个程序员的目标,简短易读.java 8中的lambda表达式的使用: 4 easy steps Download and install jdk8. Add the following ...
- 使用bootstrap时,选项框出现的意外效果,怎么办?
<label for="" style="float:right;margin-right:10%;"> <input type=&q ...
- Python网页信息采集:使用PhantomJS采集淘宝天猫商品内容
1,引言 最近一直在看Scrapy 爬虫框架,并尝试使用Scrapy框架写一个可以实现网页信息采集的简单的小程序.尝试过程中遇到了很多小问题,希望大家多多指教. 本文主要介绍如何使用Scrapy结合P ...
- 串口调试工具(Python2.7+pyserial+Tkinter)
需要与串口设备进行通讯,那么一个调试工具是必须的. 根据我自己的需要,写了个简易版本的串口调试工具: 预览图: ====================== 项目结构: COM --SerialHel ...
- Linux下MySQL安装及命令使用
先rpm -qa mysql 查看是否安装 yum list |grep mysql 查看MySQL的一些包 yum install -y mysql-server mysql mysql-devel ...
- Unix/Linux环境C编程入门教程(27) 内存那些事儿
calloc() free() getpagesize() malloc() mmap() munmap()函数介绍 calloc(配置内存空间) 相关函数 malloc,free,realloc,b ...
- xcode 5与ios 7的屏幕适配问题
#define DEVICE_IS_IPHONE5 ([[UIScreen mainScreen] bounds].size.height == 568) float height = DEVICE_ ...