【POJ】3006

Code

#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 1000001;
bool primeQ[N];
int prime[N / 10];
int cnt;
int a, d, n, cur;

inline int pos(int x){
    // 查询x在素数表中的位置,不存在时返回-1
    int t = lower_bound(prime + 1, prime + 1 + cnt, x) - prime;
    return prime[t] == x?t:-1;
}

int main(int argc, char const *argv[]){
    for(int i = 0; i < N; ++i)primeQ[i] = true;
    for(int i = 2; i < N; ++i){
        if(primeQ[i])prime[++cnt] = i;
        for(int j = 1; j <= cnt && prime[j] * i <= N; ++j){
            primeQ[i * prime[j]] = false;
            if(i % prime[j] == 0)break;
        }
    }
    scanf("%d%d%d", &a, &d, &n);
    while(!(a == 0 && d == 0 && n == 0)){
        cur = 0;
        for(int i = a; cur <= n; i += d){
            if(pos(i) != -1)++cur;
            if(cur == n){
                printf("%d
", i);
                break;
            }
        }
        scanf("%d%d%d", &a, &d, &n);
    }
    return 0;
}

Review

  • 简单考察一下线性筛
  • 不用手写二分太好了
  • 水题
原文地址:https://www.cnblogs.com/mojibake/p/15310129.html