题意:给定a,b(0<=a<=b<=10000),求a,b区间中任意一个数使n*n+n+41为素数的概率。
题解:直接打表前缀和
代码:
#include <bits/stdc++.h>
// #define int long long
#define read(x) scanf("%d", &x)
#define print(a, c) printf("%d%c", a, c)
#define dbg(x) cout << #x << "===" << x << endl
using namespace std;
const int N = 1e4 + 10;
const double eps = 1e-8;
int sum[N], l, r;
bool okPrime(int x) {
for (int i = 2; i * i <= x; i++)
if (x % i == 0) return true;
return false;
}
void init(int n) {
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + okPrime(i * i + i + 41);
}
signed main() {
init(N - 1);
while (scanf("%d%d", &l, &r) != EOF) {
double ans = 1.0 - 1.0 * (sum[r] - sum[max(0, l - 1)]) / (r - l + 1);
printf("%.2lf\n", ans * 100.0 + eps);
}
return 0;
}
/*
input:::
0 39
0 40
39 40
output:::
100.00
97.56
50.00
*/