码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 快筛质数

时间:2014-10-31 12:02:10      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:poj   数论   素数   线性筛   

题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么。


思路:这题主要考如何筛素数,线性筛。详见代码。


CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1000010
using namespace std;

int prime[MAX],primes;
bool notp[MAX];

int a,d,n;

void Pretreatment()
{
	notp[1] = true;
	for(int i = 2; i < MAX; ++i) {
		if(!notp[i])
			prime[++primes] = i;
		for(int j = 1; j <= primes && i * prime[j] < MAX; ++j) {
			notp[i * prime[j]] = true;
			if(i * prime[j] == 0)
				break;
		}
	}
}

int main()
{
	Pretreatment();
	while(scanf("%d%d%d",&a,&d,&n),a + d + n) {
		for(int now = a;; now += d) {
			if(!notp[now])	--n;
			if(!n) {
				printf("%d\n",now);
				break;
			}
		}
	}
	return 0;
}


POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 快筛质数

标签:poj   数论   素数   线性筛   

原文地址:http://blog.csdn.net/jiangyuze831/article/details/40651249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!