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 ...
随机推荐
- AtCoder Grand Contest 055 题解
A 赛时直到最后 10min 才做出这个 A 题,之前猜了一个结论一直没敢写,本来不抱啥希望 AC 的结果比赛结束时交了一发竟然 A 了,由此可见我的水平之菜/dk 考虑每次取出字符串开头字符,不妨设 ...
- RNA-seq 生物学重复相关性验证
根据拿到的表达矩阵设为exprSet 1.用scale 进行标准化 数据中心化:数据集中的各个数字减去数据集的均值 数据标准化:中心化之后的数据在除以数据集的标准差. 在R中利用scale方法来对数据 ...
- 毕业设计之mysql+主从复制+keepalived
环境介绍 mysql_VIP:192.168.111.123 mysql_M!:192.168.111.151 mysql_M2:192.168.111.152 安装mysql可以查看 两个数据库都需 ...
- Golang知识点整理
1. Golang 1.1 golang知识点大纲 1.2 指针 1.3 Golang使用validator进行数据校验及自定义翻译器 1.4 Golang GC(垃圾回收机制) 2.框架 2.1 G ...
- Shell 格式化输出printf、awk
目录 Shell 文件的格式化与相关处理 printf.awk 格式化打印printf 案例.格式化输出文件内容 输出命令echo 案例 awk数据处理工具 语法格式 处理流程 AWK内置变量 条件 ...
- 论文解读(GraRep)《GraRep: Learning Graph Representations with Global Structural Information》
论文题目:<GraRep: Learning Graph Representations with Global Structural Information>发表时间: CIKM论文作 ...
- Java Swing布局管理器GridBagLayout的使用示例 [转]
GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...
- Shell学习(七)——sort、uniq、cut、wc命令详解
Shell学习(七)--sort.uniq.cut.wc命令详解 转自:[1]linux sort,uniq,cut,wc命令详解 https://www.cnblogs.com/ggjucheng/ ...
- Multiple Inheritance in C++
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The c ...
- Oracle存储过程游标for循环怎么写
一.不带参数的游标for循环 首先编写存储过程的整体结构,如下: create or replace procedure test_proc is v_date date; --变量定义 begin ...