UVa10990 Another New Function

連結:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1931
題意:$depthphi(x)$ 函數為 $x$ 要套多少次 $phi$ 函數才會變成 $1$,求 $\Sigma_{i=m}^{n}depthphi(i)$

解法:$depthphi(x)=depthphi(phi(x))+1$,只要將範圍內的 $phi$ 函數求出,就可以得出 $depthphi(x)$ 函數。

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
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using PII = pair<int,int>;
using PLL = pair<LL, LL>;
const int INF = 1e9;
const int MXN = 2000005;
const int MXV = 0;
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FOR(i, L, R) for(int i = L; i != (int)R; ++i)
#define FORD(i, L, R) for(int i = L; i != (int)R; --i)
#define IOS cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false);

vector<int> phi(MXN, 0), a(MXN, 0), sum(MXN, 0);

void phi_table(int n)
{
phi[1] = 1;
for (int i = 2; i <= n; i++)
{
if (phi[i])
continue;
for (int j = i; j <= n; j += i)
{
if (!phi[j])
phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
}

int main()
{
IOS;
phi_table(MXN - 1);
FOR(i, 2, MXN)
{
a[i] = a[phi[i]] + 1;
sum[i] = sum[i - 1] + a[i];
}
int N, m, n;
cin >> N;
while(N--)
{
cin >> m >> n;
cout << sum[n] - sum[m - 1] << '\n';
}
}

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Recommended Posts