一、题目描述
二、算法分析说明与代码编写指导
设 f(n) = n^2 + n + 41,打表 n 从 0 到 10000 累计的质数 f(n) 的数量,记为数列 c。
每次询问 [a, b] 返回 c[b] - c[a - 1] 就是区间内含有质数的数量。但是 a、b 可以等于 0,访问 c[-1] 可能会出错。所以,改成 c[b] - c[a] + isprime(c[a])。用一个 bitset 来存储每个 f(n) 是否为质数,n = 0,1,2,……,10000。
最后输出的答案要转换成概率,而且输出的是百分比,还要加上 1e-5(其它一些数也许也可以),否则会 WA。不过我们队长说这种题面不讲但会卡精度的在老题目中才常见。
三、AC 代码(20 ms)
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<bitset>
#pragma warning(disable:4996)
using namespace std;
unsigned a, b, c[10001]; bitset<16384> isPrime;
unsigned prime[2048], _PrimeTy, MaxPrime, * prime_end = prime; bitset<16384> notprime;
inline void gen_prime() {
decltype(_PrimeTy) n = notprime.