TOJ 3105.A Way To Find Primes(埃式筛法)
来源:互联网 发布:手机上怎么看淘宝等级 编辑:程序博客网 时间:2024/06/01 12:24
题目链接:http://acm.tju.edu.cn/toj/showp3105.html
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 1481 Accepted Runs: 778
Description
Given a integer p > 1, if p can be devided only by 1 and itself, we say that p is a prime number. For example, 31 can only be devided by 1 and 31, so 31 is a prime number; 12 can be devided by six numbers: 1, 2, 3, 4, 6, 12, so 12 is not a prime number. The smallest ten prime number is 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.
Eratosthenes was a Greek mathematician, astronomer, and geographer. He invented a method for finding prime numbers that is still used today. This method is called Eratosthenes'Sieve. A sieve has holes in it and is used to filter out the juice. Eratosthenes's sieve filters out numbers to find the prime numbers.
Eratosthenes's sieve can be used as follows to find all prime numbers smaller than or equal to a given number n:
Step1 : Put all integers between 2 and n (include 2 and n) on Eratosthenes's sieve.
Step2 : Sellect the smallest number on the sieve, suppose it is m, then m must be a prime.
Step3 : Filter out all the integers which can be devided by m from Eratosthenes's sieve.
Step4 : If m * m ≤ n, go to Step 2.
Step5 : Integers remains on the sieve are all prime numbers.
For example, if we want to find all primes between 2 and 20 using Eratosthenes's sieve,Put all integers between 2 and 20 on Eratosthenes's sieve.
Sieve : 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20Prime bin :Sellect the smallest number, it's 2, so it is a prime. Then filter out all integers can be devided by 2:Sieve : 3, 5, 7, 9, 11, 13, 15, 17, 19Prime bin : 2Sellect the smallest number, it's 3, so it is also a prime. Then filter out all integers can be devided by 2:Sieve : 5, 7, 11, 13, 17, 19Prime bin : 2, 3Sellect the smallest number, it's 5, so it is also a prime. Then filter out all integers can be devided by 5:Sieve : 7, 11, 13, 17, 19Prime bin : 2, 3, 5Because 5 * 5 = 25 > 20, so all numbers remains on Eratosthenes's sieve are prime, put all of them into prime bin:Sieve :Prime bin : 2, 3, 5, 7, 11, 13, 17, 19Now the task is, given a number k, output the k-th smallest prime number. (You may use any way you want to solve this problem as it is correct)
Input
The first line is an integer n, the number of test cases, n cases follows. For Each test case has an single integer k (k > 1).
Output
A single integer per line, the k-th prime number. It is guaranted that the correct answer is smaller than 100000.
Sample Input
41101001000
Sample Output
2295417919
Hint
If you want to use an large arrays, Please define them as global variables. For example, if I want to use an array named arr[]:
#include .........int arr[1000000];int main () { ......}
Source:
水题
#include <stdio.h>#include <string.h>bool isPrime[100001];int prime[10000];void Prime(int n){memset(isPrime,true,sizeof(isPrime));int num=0;isPrime[0]=isPrime[1]=false;for(int i=2;i<=n;i++){if(isPrime[i]){prime[++num]=i;for(int j=2*i;j<=n;j+=i)isPrime[j]=false;}}}int main(){int n,num;scanf("%d",&n);Prime(100000);while(n--){scanf("%d",&num);printf("%d\n",prime[num]);}}
- TOJ 3105.A Way To Find Primes(埃式筛法)
- hdu1593(find a way to escape)
- find a way to escape
- HDOJ题目1593find a way to escape(数学)
- find a way to escape(物理真白学了)
- HDU 1593 find a way to escape(角速度)
- hdu Find a way to escape (几何)
- hdu 1593 find a way to escape
- HDU 1593 find a way to escape
- hdu 1593 find a way to escape
- hdu 1593 find a way to escape
- HDU 1593 find a way to escape
- hdu 1593 find a way to escape
- HDU 1593 find a way to escape
- HDU 1593: find a way to escape
- HDU 1593 find a way to escape
- hdu find a way to escape(1593)
- hdu 1593 find a way to escape
- 零java基础搞定微信Server_2_Servlet基础
- 谈谈Runtime类中的freeMemory,totalMemory,maxMemory等几个方法
- 社会环保意识调查新闻稿
- Python函数式编程指南(一):概述
- Java小知识
- TOJ 3105.A Way To Find Primes(埃式筛法)
- 关于解决line 1: #!/bin/bash: No such file or directory的问题
- 栈的压入、弹出序列
- 【挫纪】多重背包问题的优化细节
- 栅栏密码
- 零java基础搞定微信Server_3_搭建微信Server本地开发环境
- UVa OJ 1609 - Foul Play
- 变量和常量
- UITextField 知识整理