Test instructions: Give you two numbers, A, B, to find out how many numbers in [A, b] satisfy F (n) =n*n+n+41 is a prime number.
Solving: preprocessing except prefix and, direct
#include <cstring> #include <cstdio> #include <iostream> #include <algorithm> #include < cmath> #include <stack> #include <vector> #define N 10010using namespace Std;int n;int s[n];bool ok (int x) { C0/>if (x==1| | X==0) return false; for (int i=2; i*i<=x; i++) { if (x%i==0) return false; } return true;} void Init () {for (int i=0; i<n; i++) { int k=i*i+i+41; if (ok (k)) s[i]=1; else s[i]=0; } for (int i=1; i<n; i++) s[i]+=s[i-1];} int main () { //freopen ("test.in", "R", stdin); Init (); int a, B; while (~SCANF ("%d%d", &a,&b)) { int x=b-a+1; int y=s[b]; if (a!=0) y-=s[a-1]; Double ans=y*100.0/(x*1.0) +1e-5;//precision printf ("%.2f\n", ans); return 0;}
UVA 10200 Prime Time