HDU 2161 Primes (素数筛选法)
题意:输入一个数判断是不是素数,并规定2不是素数。
析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了。。。
不说了,直接上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std;
typedef long long LL;
const int maxn = 16000 + 10;
int prime[maxn]; void init(){
memset(prime, 0, sizeof(prime));
for(int i = 2; i < sqrt(maxn+0.5); ++i) if(!prime[i])
for(int j = i * i; j < maxn; j += i)
if(!prime[j]) prime[j] = 1;
} int main(){
init();
int n, kase = 1;;
while(scanf("%d", &n), n > 0){
printf("%d: ", kase++);
if(1 == n || 2 == n || prime[n]) puts("no");
else puts("yes"); }
return 0;
}
HDU 2161 Primes (素数筛选法)的更多相关文章
- HDOJ(HDU) 2161 Primes(素数打表)
Problem Description Write a program to read in a list of integers and determine whether or not each ...
- 数学--数论--HDU 2136(素数筛选法)
Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...
- POJ 3978 Primes(素数筛选法)
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- HDU_2136——最大质因数,素数筛选法
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。
废话不多说,先上题目. 51nod Codeforces 两个其实是一个意思,看51nod题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后 ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
随机推荐
- jsfl 发布保存关闭
fl.getDocumentDOM().publish(); fl.getDocumentDOM().save(); fl.getDocumentDOM().close();
- ABAP-BarCode-3-调用第三方控件BarTender实现打印
1.BarTender软件安装及注册 2.BarTender设置好打印模板 3.ABAP生成TXT文件放置FTP服务器指定文件夹 4.BarTender轮询FTP服务器文件夹中的TXT,并按照模板打印 ...
- UI5-文档-4.19-Reuse Dialogs
在此步骤中,我们将扩展重用概念,并在组件级别调用对话框. 在步骤16中,我们创建了一个对话框作为片段,以使其可跨视图或跨整个应用程序重用.但是我们将检索对话框实例和分别打开和关闭对话框实例的逻辑放置在 ...
- SpringBoot 监控管理模块actuator没有权限的问题
SpringBoot 1.5.9 版本加入actuator依赖后, 访问/beans 等敏感的信息时候报错,如下 Tue Mar 07 21:18:57 GMT+08:00 2017 There wa ...
- python os模块常用命令
python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()——判断现在正在实用的平台,Window ...
- 利用css和javascript实现简单的计算器
<!doctype html> <html> <head> <!--声明当前页面的编码集--> <meta http-equiv="Co ...
- ThreadPoolExecutor的execute源码分析
上一篇文章指出,ThreadPoolExecutor执行的步骤如下: 向线程池中添加任务,当任务数量少于corePoolSize时,会自动创建thead来处理这些任务: 当添加任务数大于corePoo ...
- luoguP1004 方格取数(四维DP)
题目链接:https://www.luogu.org/problemnew/show/P1004 思路: 这道题是四维DP的模板题,与luoguP1006传纸条基本相似,用f[i][j][k][l]表 ...
- Java中Generics的使用
1.Java的Generics与C++的Template由于Java的Generics设计在C++的Template之后,因此Java的Generics设计吸取Template的很多经验和教训.首先, ...
- asp.net后台解析JSON,并将值赋给对象
示例代码如下: using System; using System.Collections.Generic; using System.Web.Script.Serialization; publi ...