-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10200.cpp
86 lines (76 loc) · 1.64 KB
/
P10200.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <stdio.h>
#include <bitset>
#define PRIME_LEN 11000
class PrimeHandler {
std::bitset<PRIME_LEN> primes;
public:
void init() {
primes.set();
// Sieve primes:
for(int i = 0; i*i < PRIME_LEN; ++i) {
if(!primes[i])
continue;
// Mark all uneven multiples as non-prime:
int basePrime = 1+2*(i+1);
for(int multiple = 3; true; multiple += 2) {
int notAPrime = basePrime*multiple;
int notAPrimeI = notAPrime/2-1;
if(notAPrimeI >= PRIME_LEN)
break;
primes.set(notAPrimeI, false);
}
}
}
bool isPrime(long n) const {
if(n == 2)
return true;
if(n < 2 || (n%2==0))
return false;
return primes[n/2-1];
}
long nextPrime(long n) const {
long ni = n/2;
while(!primes[ni]) {
++ni;
}
return 1+(ni+1)*2;
}
};
void die() {
int *d = NULL;
d[0] = 555;
}
int main() {
int primesUntil[10020]; // idx => primes until and including Euler(idx-1)
PrimeHandler ph;
ph.init();
for(int i = 0; i < 40; ++i)
primesUntil[i] = i+1;
int I = 39;
long a, b;
while(std::cin >> a >> b) {
if(a > b)
std::swap(a,b);
if(a < 0 || b > 10000)
die();
while(I < b) {
++I;
long eu = I*I+I+41;
bool prime = true;
long tryPrime = 3;
while(tryPrime * tryPrime <= eu) {
if(eu % tryPrime == 0) {
prime = false;
break;
}
tryPrime = ph.nextPrime(tryPrime);
}
primesUntil[I] = primesUntil[I-1] + (prime ? 1 : 0);
}
int lowPrimes = 0;
if(a > 0)
lowPrimes = primesUntil[a-1];
printf("%.2f\n", 1e-6+(100.0*(primesUntil[b]-lowPrimes))/((double)(b-a+1)));
}
}