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 ...
随机推荐
- LeetCode 252. Meeting Rooms (会议室)$
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 使用Lock锁生产者消费者模式
package com.java.concurrent; import java.util.concurrent.locks.Condition; import java.util.concurren ...
- c++学习笔记---03---从一个小程序说起2
从一个小程序说起2 要求:编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行中的任何位置.当用户按下键盘上的"Enter"键时,数据输入 ...
- Android 开发笔记___实战项目:购物车
购物车的应用很广泛,电商app基本上都有它的身影.由于它用到了多种存储方式,通过项目对数据的存储有更高层次的了解. 1.设计思路 首先看看购物车的外观.第一次进入时里面是空的,去购物页面加入购物车以后 ...
- Python文件夹备份
Python文件夹备份 import os,shutil def file_copy(path1,path2): f2 = [filename1 for filename1 in os.listdir ...
- Jquery中attr 和 prop的区别和联系
昨天在选择借款方类型的时候总是会出现选择要点两次的现象,比如点击公司,第一次点击选择公司,没有选中,必须在次点击才可以选中,总感觉是有点延迟加载的意思,后来审查元素, 发现是redio元素,这样的话就 ...
- SpringMVC---Method
GET 平时网页的一些基本的URL都是GET请求的,用于执行查询操作. 但是由于GET中URL是有长度的限制的,而GET会把所有的参数都放在URL中 因此就会有下面的问题: 1 数据都明文暴露,用户可 ...
- Android开发之漫漫长途 番外篇——自定义View的各种姿势1
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- shell脚本 案例
1,写一个循环,ping整个子网的ip. 使用while循环 #!/bin/bash ip=223 while [ "$ip" -ne "239" ] ...