CF17A Noldbach problem 题解
Content
若一个素数可以用比它小的相邻的两个素数的和加 \(1\) 表示,那么称这个素数为"好素数"。 给定两个正整数 \(n,k\),问从 \(2\) 到 \(n\) 的好素数个数是否 \(\geqslant k\)。
数据范围:\(2\leqslant n\leqslant 1000,0\leqslant k\leqslant 1000\)。
Solution
直接通过埃氏筛得到 \(1000\) 以内的素数,再通过直接暴力枚举预处理出 \(1000\) 以内的“好素数”,最后再遍历 \(2\) 到 \(n\) 求得这段区间以内“好素数”的数量,判断即可。
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
int n, k, ans, isprime[1007], primes[1007], goodprimes[1007];
int main() {
for(int i = 2; i <= 1000; ++i) isprime[i] = 1;
for(int i = 2; i <= 1000; ++i)
if(isprime[i]) {
primes[++primes[0]] = i;
for(int j = i * 2; j <= 1000; j += i)
isprime[j] = 0;
}
for(int i = 1; i <= primes[0]; ++i)
for(int j = 1; j < i - 1; ++j)
if(primes[i] == primes[j] + primes[j + 1] + 1) {
goodprimes[primes[i]] = 1;
break;
}
scanf("%d%d", &n, &k);
for(int i = 2; i <= n; ++i) {
if(ans == k) return printf("YES"), 0;
if(goodprimes[i]) ans++;
}
if(ans == k) return printf("YES"), 0;
printf("NO");
return 0;
}
CF17A Noldbach problem 题解的更多相关文章
- cf17A Noldbach problem(额,,,素数,,,)
题意: 判断从[2,N]中是否有超过[包括]K个数满足:等于一加两个相邻的素数. 思路: 枚举. 也可以:筛完素数,枚举素数,直到相邻素数和超过N.统计个数 代码: int n,k; int prim ...
- Codeforces Beta Round #17 A - Noldbach problem 暴力
A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...
- Noldbach problem
Description Noldbach problem time limit per test: 2 seconds memory limit per test: 64 megabytes inpu ...
- POJ2826:An Easy Problem?!——题解(配特殊情况图)
http://poj.org/problem?id=2826 题目大意:给两条线,让它接竖直下的雨,问其能装多少横截面积的雨. ———————————————————————————— 水题,看题目即 ...
- HDU 1016 Prime Ring Problem 题解
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- HDU 4143 A Simple Problem 题解
题目 For a given positive integer n, please find the saallest positive integer x that we can find an i ...
- UVA101 The Blocks Problem 题解
题目链接:https://www.luogu.org/problemnew/show/UVA101 这题码量稍有点大... 分析: 这道题模拟即可.因为考虑到所有的操作vector可最快捷的实现,所以 ...
- [CF-GYM]Abu Tahun Mod problem题解
前言 这道题比较简单,但我还是想了好一会 题意简述 Abu Tahun很喜欢回文. 一个数组若是回文的,那么它从前往后读和从后往前读都是一样的,比如数组\(\left\{1\right\},\left ...
- [NOIP模拟测试9]题(Problem) 题解 (组合数全家桶+dp)
达哥送分给我我都不要,感觉自己挺牛批. $type=0:$ 跟visit那题类似,枚举横向移动的步数直接推公式: $ans=\sum C_n^i \times C_i^{\frac{i}{2}} \t ...
随机推荐
- 【JavaSE】JDK配置
Java开发环境配置 2020-09-10 08:32:20 by冲冲 1. Windows7安装JDK 1.1 下载JDK ① 下载地址:http://www.oracle.com/techne ...
- linux命令-压缩数据
linux文件压缩工具:bzip2 文件扩展名 .bz2 compress 文件扩展名 .Z linux上很少看到了 uncompress解压 gzip 文件扩展名,.gz,gzip压缩文件,gzca ...
- Java设计模式之(十三)——模板方法模式
1.什么是模板模式? Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. ...
- C/C++ Qt TabWidget 实现多窗体创建
在开发窗体应用时通常会伴随分页,ToolBar组件可以实现顶部工具栏菜单,每一个ToolBar组件关联到一个TabWidget组件的Tab标签内,这样我们就可以实现一个复杂的多窗体分页结构,此类结构也 ...
- Golang知识点整理
1. Golang 1.1 golang知识点大纲 1.2 指针 1.3 Golang使用validator进行数据校验及自定义翻译器 1.4 Golang GC(垃圾回收机制) 2.框架 2.1 G ...
- excel--CLEAN()函数,解决为什么看着相同的字符串但是len()长度不同
CLEAN()函数能够有效解决去除字符串中隐藏的字符(这些字符是TRIM()去除不掉的)
- Mybatis批量添加、更新小结
虽然是很基础的东西,不过难免会忘记,所以写个笔记巩固一下,顺便分享. 实体类: @Data public class EventOrder { private Long id; private ...
- FTP 文件传输服务
昨晚心血来潮,尝试用python写了一个ftp文件传输服务,可以接收指令,从远程ftp服务器同步指定目录数据,最后没用上,开源出来. https://github.com/jadepeng/ftp_t ...
- 【Reverse】DLL注入
DLL注入就是将dll粘贴到指定的进程空间中,通过dll状态触发目标事件 DLL注入的大概流程 https://uploader.shimo.im/f/CXFwwkEH6FPM0rtT.png!thu ...
- C++ 继续(3n+1)猜想
1005 继续(3n+1)猜想 (25分) 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过 ...