HDU 1216 Assistance Required 埃拉托色尼色筛法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1216
思路:色筛法
代码(1):
#include<iostream>//--------1216 HDU 埃拉托色尼筛选法
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<vector>
using namespace std;
#define Max 40000
bool b[Max];
__int64 a[Max], c;
void fun()
{
__int64 i, j, count;
c = ;
memset(b, true, sizeof(b));
b[] = ;
for (i = ; i < Max; ++i)
{
if (b[i])
{
a[++c] = i;
count = i;
for (j = i+; j < Max; ++j)
{
if (b[j] == true)
count--;
if (count == )
{
b[j] = false;
count = i;
}
}
}
}
}
int main()
{
fun();
int n;
while (scanf("%d", &n) != EOF)
{
if (n == )
break;
else
printf("%d\n", a[n]);
}
return ;
}
基本类似的另外一种:
#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define Max 40000
__int64 ans[Max];
__int64 pop[];
void dfs()
{
__int64 i, j, count;
for (i = ; i < Max; i++)
{
ans[i] = i;
}
for (i = ; i < Max; i++)
{
if (ans[i] != )
{
count = i;
for (j = i + ; j < Max; j++)
{
if (ans[j] != )
count--;
if (count == )
{
ans[j] = ;
count = i;
}
}
} }
j = ;
for (i = ; i <= ; i++)
{
while (ans[j] == )
{
j += ;
}
if (ans[j] != )
{
pop[i] = ans[j];
j += ;
}
}
}
int main()
{
dfs();
int n;
while (scanf("%d", &n)&&n)
{
printf("%d\n", pop[n]);
}
return ;
}
HDU 1216 Assistance Required 埃拉托色尼色筛法的更多相关文章
- HDU 1216 Assistance Required(暴力打表)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1216 Assistance Required Time Limit: 2000/1000 MS (Ja ...
- 2015 HDU 多校联赛 5317 RGCDQ 筛法求解
2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目 http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...
- 埃拉托色尼(Eratosthenes)筛法
用筛选法求1—100之内的素数(此法难度的话,方法可以不界定:能完成求1—100之内的素数即可). 在一张纸上写上1到100全部整数,然后逐个判断它们是否是素数,找出一个非素数,就把它挖掉,最后 ...
- SP1798 ASSIST - Assistance Required 题解
Content 有一个足够长的数列 \(a\),是一个首项为 \(2\),公差为 \(1\) 的等差递增数列.另有一个初始为空的数列 \(b\). 重复进行如下操作: 假设当前数列 \(a\) 第一项 ...
- 2018年暑假ACM个人训练题7 题解报告
A:HDU 1060 Leftmost Digit(求N^N的第一位数字 log10的巧妙使用) B:(还需要研究一下.....) C:HDU 1071 The area(求三个点确定的抛物线的面积, ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 给swift程序猿留下深刻印象的10个Swift代码
通过使用单行代码完成同样的 10 个练习,我们来看看 Swift 和其他语言之间的较量. 将数组中每个元素的值乘以 2 使用map来实现 var arr = [1,2,3,4]; var newArr ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
随机推荐
- Memcached查找命令
Memcached各个查找命令的语法格式都类似,且有相同的参数和参数含义,先将可能出现的各个参数的意义说明如下 key:键值 key-value 结构中的 key,用于查找缓存值. noreply(可 ...
- 在 ASP.NET Core 中使用 SignalR
https://weblogs.asp.net/ricardoperes/signalr-in-asp-net-core 作者:Ricardo Peres 译者:oopsguy.com 介绍 Sign ...
- datagrid参数queryParams--easyUI
datagrid参数queryParams--easyUI Html <div region="center" border="false&qu ...
- 中高级JavaScript易错面试题
写出下题的输出 1.函数的实参与形参length var length = 10; function fn() { console.log(this.length); } var obj = { le ...
- Tinyhttpd阅读笔记
1.简介 tinyhttpd是一个开源的超轻量型Http Server,阅读其源码,可以对http协议,微型服务器有进一步的了解. 源码链接: 参考博客:tinyhttpd源码分析 2.笔记 ---- ...
- 30个CSS3选择器的应用
或许大家平时总是在用的选择器都是: id class 以及标签选择器.可是这些还远远不够,为了在开发中更加得心应手,本文总结了30个CSS3选 或许大家平时总是在用的选择器都是:#id .class ...
- SQL Server Backup & Restore
USE [master]; GO CREATE DATABASE test; GO CREATE DATABASE test2; GO BACKUP DATABASE test TO DISK = ' ...
- 阿里云centos 搭建SVN
1. 安装 先进入想安装的目录,执行 yum install subversion 2. 创建SVN目录 mkdir -p /var/svn/svnrepos ,然后创建版本库 svnadmin c ...
- selenium页面元素操作(简易版)
介绍一下,这是处理页面元素的基本方法,@selenium 发送文字 element.send_keys(keys_to_send) 单击 element.click() 提交表单 el ...
- 微信支付——openid获取不到
1.写微信支付遇到状况,通过wx.login获取code,然后向微信服务器获取openid,获取失败:{"errcode":40029,"errmsg":&qu ...