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. oracle使用还原段的目的和还原数据的管理方法及还原段的类型

    一.引入还原段主要有3个目的: 1.事务回滚:主要是针对rollback语句起作用 2.事务恢复:非正常关闭数据库即非保留事务级关闭数据库(abort.immediate)或者数据库instance崩 ...

  2. Linux环境下常用的SSH命令

    目录操作: rm -rf mydir /*删除mydir目录*/ mkdir dirname /*创建名为dirname的目录*/ cd mydir /*进入mydir目录*/ cd – /*回上一级 ...

  3. shell全备份脚本(借鉴别人的,在其基础上修复完善了bug)

    #!/bin/bash # Shell script to backup MySql database # Last updated: Aug - MyUSER="root" # ...

  4. laravel artisan 命令工具

    //全局相关 php artisan:显示详细的命令行帮助信息,同 php artisan list php artisan –help:显示帮助命令的使用格式,同 php artisan help ...

  5. ArrayList的contains方法(转)

    今天在用ArrayList类的caontains方法是遇到了问题,我写了一个存放User类的ArrayList  但在调用list.contains(user)时总是返回false. 去看了下Arra ...

  6. Eclipse Useful Plugins Links

    1.maven for eclipse http://m2eclipse.sonatype.org/sites/m2e 2. svn1.6  for ecipse http://subclipse.t ...

  7. NetFlow

    供应商 支持的流 设备列表 Cisco NetFlow Cisco - 800.1700.2600.1800.2800.3800.4500.6500.7200.7300.7500.7600.10000 ...

  8. 手动升级Delphi控件时,修改inc文件的办法

    以MustangPeakCommonLib.exe控件为例,想让它支持Delphi2010,就需要在D:\Program Files\Common Library\Mustangpeak\Common ...

  9. Delphi调用安装驱动sys的单元

    unit SysDriver; interface uses windows, winsvc; // jwawinsvc; Type TSysDriver = class(TObject) priva ...

  10. linux脚本实例之while

    写一个脚本,执行后,打印一行提示“Please input a number:",要求用户输入数值,然后打印出该数 值,然后再次要求用户输入数值.直到用户输入"end"停 ...