质数筛选 poj3006 Dirichlet's Theorem on Arithmetic Progressions

。。今晚练习简直就是模板测试专题,又测了两个质数模板,,发现线性筛的那个模板弄错了,,于是改了一下,修正过来了


因为这题已经告诉你了答案不会超过1e6,所以就把1e6的质数全部筛选出来,就可以通过O(1)方法判断是否是质数,然后暴力枚举质数个数就可以了

#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
#include<algorithm>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 1000000 + 5;
const int INF = 0x3f3f3f3f;

int prime[100000], rear;
bool vis[MX];

void prime_init() {
    vis[1] = 1;
    rear = 0;
    for(int i = 2; i < MX; i++) {
        if(vis[i]) continue;
        prime[++rear] = i;

        if((LL)i * i >= MX) continue;
        for(int j = i * i; j < MX; j += i) {
            vis[j] = 1;
        }
    }
}

int main() {
    prime_init();
    int a, b, th;
    while(~scanf("%d%d%d", &a, &b, &th), a + b + th) {
        int cnt = 0, ans;
        for(int i = a; ; i += b) {
            if(!vis[i]) {
                cnt++;
                if(cnt == th) {
                    ans = i;
                    break;
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(质数筛选 poj3006 Dirichlet's Theorem on Arithmetic Progressions)