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. (转+原)android获取系统时间

    参考的网站如下: http://c.biancheng.net/cpp/html/144.html http://www.linuxidc.com/Linux/2012-03/55909.htm 代码 ...

  2. 关于json对象的遍历

    json格式的数据越来越多的在web开发中起到重要作用.下面介绍对于json对象和数组经常用到解析方法. var obj ={”name”:”冯娟”,”password”:”123456″,”depa ...

  3. 学习《Javascript权威指南》的第二章笔记

    1.Javascript区分大小写,但是HTML不区分大小写 2.JS会忽略标识之间的空格,多数情况下也会忽视换行符,所以要采用 整齐.一致的编码风格 3.//用作结尾的注释,/* 和 */可以当跨行 ...

  4. Windows -DOS 下Yii创建应用及出错处理

    Win7下,以管理员身份运行cmd.exe 本例是在wamp环境下:切换到d盘,再切换到framework目录.............1--C:\windows\system32>d: 2-- ...

  5. hdu 4906 3-idiots fft

    题目链接 n个火柴棍取3个, 问能组成三角形的概率是多少. kuangbin大神的博客写的很详细了..http://www.cnblogs.com/kuangbin/archive/2013/07/2 ...

  6. 寻找素数对(hd1262)

    寻找素数对 点我 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. Oracle左连接、右连接、全外连接

    Oracle  外连接 (1)左外连接 (左边的表不加限制)(2)右外连接(右边的表不加限制)(3)全外连接(左右两表都不加限制) 外连接(Outer Join) outer join则会返回每个满足 ...

  8. 只有勇敢的人、鲁莽的人和绝望的人才会追求大的变革 – D.J. Anderson

    只有勇敢的人.鲁莽的人和绝望的人才会追求大的变革 – D.J. Anderson http://www.cnblogs.com/lchrennew/p/Why-The-Future-Of-Agile- ...

  9. Linux 上的数据可视化工具

    Linux 上的数据可视化工具 5 种开放源码图形化工具简介 Linux® 上用来实现数据的图形可视化的应用程序有很多,从简单的 2-D 绘图到 3-D 制图,再到科学图形编程和图形模拟.幸运的是,这 ...

  10. CDOJ 631 敢说敢做 记忆化搜索and动规

    //跟沈爷学的 传送门http://www.cnblogs.com/Xiper/p/4639636.html #include<cstdio> #include<iostream&g ...