1. Title link:
UVA-11426
2. The main idea of the topic:
3. Analysis:
Posting a solution to the problem of Juju is naturally not something I can think of...
Another thing that has bothered me for a long time is how to calculate F[n] quickly.
As written in the Juju blog
Ben Konjac only thought of the complexity calculation F[], and was crying stupidly by myself _(:3」∠)_
The positive solution is to calculate the contribution to F[] when gcd(x, j) = i. It is easy to get i | j, and the complexity is
4. Code implementation:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = (int)4e6;
bool is_prime[M + 5];
int cnt, prime[M + 5];
int phi[M + 5];
ll sum[M + 5];
void init()
{
cnt = 0;
memset(is_prime, 1, sizeof(is_prime));
is_prime[0] = is_prime[1] = 0;
for(int i = 2; i <= M; ++i)
{
if(is_prime[i])
{
prime[++cnt] = i;
phi[i] = i - 1;
}
for(int j = 1; j <= cnt && i * prime[j] <= M; ++j)
{
is_prime[i * prime[j]] = 0;
if(i % prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
int main()
{
init();
for(int i = 1; i <= M; ++i)
{
for(int j = 2 * i; j <= M; j += i)
{
sum[j] += 1ll * i * phi[j / i];
}
}
for(int i = 1; i <= M; ++i)
sum[i] += sum[i - 1];
int n;
while(~scanf("%d", &n) && n)
{
printf("%lld\n", sum[n]);
}
return 0;
}