-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriemannVsMertens_UVA10738.cpp
91 lines (77 loc) · 1.83 KB
/
riemannVsMertens_UVA10738.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
87
88
89
90
91
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("riemannVsMertens_UVA10738.in");
ofstream fout("riemannVsMertens_UVA10738.out");
const int N = 1000000;
int main()
{
vector<bool> isPrime(N + 1, true);
isPrime[0] = false, isPrime[1] = false;
vector<int> primes;
for (long long i = 2; i <= N; ++i)
{
if (isPrime[i] == true)
{
primes.push_back(i);
for (long long j = i * i; j <= N; j += i)
{
isPrime[j] = false;
}
}
}
int size0 = primes.size();
vector<int> mu(N + 1, -2), M(N + 1, 0);
mu[1] = 1; M[1] = 1;
for (long long n = 2; n <= N; ++n)
{
long long temp = n, _count = 0;
for (int i = 0; primes[i] * primes[i] <= temp; ++i)
{
long long now = primes[i];
if (temp % (now * now) == 0)
{
mu[n] = 0;
break;
}
else if (temp % now == 0)
{
++_count;
temp /= now;
}
}
if (mu[n] == -2)
{
if (temp > 1)
{
++_count;
}
if (_count % 2 == 0)
{
mu[n] = 1;
}
else
{
mu[n] = -1;
}
}
M[n] = M[n - 1] + mu[n];
}
while (true)
{
int n = 0;
fin >> n;
if (n == 0) break;
fout << setw(8) << n << setw(8) << mu[n] << setw(8) << M[n] << '\n';
}
return 0;
}