Description
Noldbach problem
time limit per test: 2 seconds
memory limit per test: 64 megabytes
input: standard input
output: standard output

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.

Input

The first line of the input contains two integers n (2 ≤ n ≤ 1000) and k (0 ≤ k ≤ 1000).

Output

Output YES if at least k prime numbers from 2 to n inclusively can be expressed as it was described above. Otherwise output NO.

Sample test(s)
input
27 2
output
YES
 
 
 
 
 
 
input
45 7
output
NO
 
 
 
 
 
 
题解:
  如果一个素数可以用三个素数之和表示,其中一个为1,另外两个为相邻的素数,则计数一次。
  从2到n,存在上述的素数不少于k个,输出YES,否则输出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 ;
}

Problem -17A - Codeforces

转载请注明出处:http://www.cnblogs.com/michaelwong/p/4133219.html

Noldbach problem的更多相关文章

  1. Codeforces Beta Round #17 A - Noldbach problem 暴力

    A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...

  2. cf17A Noldbach problem(额,,,素数,,,)

    题意: 判断从[2,N]中是否有超过[包括]K个数满足:等于一加两个相邻的素数. 思路: 枚举. 也可以:筛完素数,枚举素数,直到相邻素数和超过N.统计个数 代码: int n,k; int prim ...

  3. CF17A Noldbach problem 题解

    Content 若一个素数可以用比它小的相邻的两个素数的和加 \(1\) 表示,那么称这个素数为"好素数". 给定两个正整数 \(n,k\),问从 \(2\) 到 \(n\) 的好 ...

  4. Codeforces Beta Round #17 A.素数相关

    A. Noldbach problem Nick is interested in prime numbers. Once he read about Goldbach problem. It sta ...

  5. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  6. 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 ...

  7. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  8. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  9. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

随机推荐

  1. 百度地图坐标转换API和地图API

    利用百度地图的服务将经纬度转换为米单位坐标 using System; using System.Collections.Generic; using System.Linq; using Syste ...

  2. Java编程思想——类型信息(RTTI)

    一.概念 编译时已知的到所有的类型:就是在写代码阶段就确定是这个类型了,当运行程序的时候,类型是不可改变的 举例:List<String> str = new ArrayList();   ...

  3. java性能优化技巧

    在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. 1. 尽量使用final修饰符. 带有final修饰符的类是不可派生的. ...

  4. openstack安装记录(一)环境准备

    参考文献: 官方文档 http://docs.openstack.org/mitaka/zh_CN/install-guide-rdo/index.html 最小实例: 控制节点: 1 处理器, 4 ...

  5. flask-script 安装问题

    > 昨天开始看 flask,第二章最后提到使用 Flask-Script 支持命令行选项,但是用书上的方法安装却出现了问题. 错误信息: 注意最后两行: Could not find a ver ...

  6. linux shell if语句

    #!/bin/bash read -p "please input Y/N" keyWord if [ "$keyWord" == "Y" ...

  7. fragment低版本

    http://bbs.csdn.net/topics/390271980 Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持.以前的版本必须用兼容模式开发,本人在网上找了大量资料, ...

  8. C11 memory_order

    概念: 摘录自:http://preshing.com/20120913/acquire-and-release-semantics/ Acquire semantics is a property ...

  9. Linux基本命令(开发常用的、电脑常用的)

    一.开发常用的 ###转自:http://www.weixuehao.com/archives/25#usergroup 转自:http://blog.csdn.net/ljianhui/articl ...

  10. cp | mv | rm

    linux下文件的复制.移动与删除命令为:cp,mv,rm 一.文件复制命令cp 命令格式: cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [opt ...