求 ∑ni=1∑ij=1gcd(i,j) 的值。
gcd(i∗d,j∗d)=d∗gcd(i,j)
设
gcd(i,j)=1
, 不大于
i
且与
所以 ∑ni=1∑ij=1gcd(i,j)=∑nd=1∑ndi=1φ(i)∗d=∑nd=1d∗∑ndi=1φ(i)
枚举
d
,预处理求出
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
#define REP(__i,__start,__end) for(int __i = (__start); __i <= (__end); __i++)
const int size = 1e7 + 50;
int n, phi[size];
long long sum[size];
int prime[size], tot;
bool check[size];
void prework()
{
sum[1] = phi[1] = 1;
REP(i, 2, n)
{
if(!check[i])
{
prime[++tot] = i;
phi[i] = i - 1;
}
REP(j, 1, tot)
{
if((long long)i * prime[j] > n) break;
check[i * prime[j]] = true;
if(i % prime[j])
{
phi[i * prime[j]] = phi[i] * phi[prime[j]];
}
else
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
}
sum[i] = sum[i - 1] + phi[i];
}
}
void solve()
{
long long ans = 0;
REP(i, 1, n) ans += sum[n / i] * i;
write(ans);
}
int main()
{
freopen("A.in","r",stdin);
freopen("A.out","w",stdout);
read(n);
prework();
solve();
fclose(stdin);
fclose(stdout);
return 0;
}