POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 素数

题意:给出一列等差数列。等差数列的起点a与公差d互素。求数列的第n个素数。

#include <cmath>
#include <iostream>
using namespace std;

int Prime[1000000];

int main()
{
	int i, j;
	memset(Prime,-1,sizeof(Prime));
	Prime[0] = Prime[1] = 0;
	for ( i = 2; i <= 1000; i++ )
	{
		if ( Prime[i] )
			for ( j = 2; i * j <= 1000000; j++ )
				Prime[i*j] = 0;
	}

	int a, d, n, counter;
	while ( cin >> a >> d >> n )
	{
		counter = 0;
		if ( a == 0 && d == 0 && n == 0 ) break;
		for ( i = a; counter != n; i += d )
			if ( Prime[i] )
				counter++;
		cout << i - d << endl;
	}
	return 0;
}


你可能感兴趣的:(POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 素数)