返回顶部

POJ - 3006 - Dirichlet's Theorem on Arithmetic Progressions = 水题

http://poj.org/problem?id=3006

给一个等差数列,求其中的第n个质数,答案保证不超过1e6。n还特别小?!!!

埃筛之后暴力。

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

bool np[1000005];

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    np[1] = 1;
    for(int i = 2; i <= 100000; ++i) {
        if(np[i])
            continue;
        else {
            for(int j = i + i; j <= 1000000; j += i)
                np[j] = 1;
        }
    }
    int a, d, n;
    while(cin >> a >> d >> n) {
        if(a == 0 && d == 0 && n == 0)
            break;
        while(1) {
            if(!np[a])
                n--;
            if(n == 0) {

                cout << a << endl;
                break;
            }
            a += d;
        }
    }
}
posted @ 2019-10-22 23:20  Inko  阅读(79)  评论(0编辑  收藏  举报