2018 Multi-University Training Contest 10 - Count
数论,gcd
gcd(i+j, i-j) = gcd(2*i, i+j)
把i+j看出一个整体,得到范围为[i+1...2*i-1]
再看phi[2i]的意义,即[1...2i-1]上与2*i互质的数的个数
i+j的范围刚好是其一半,又引理可知欧拉函数在这个范围内均分成两半。
所以当i为奇数时,2和i互质,根据积性函数的性质,phi[2*i] = phi[2] * phi[i] = phi[i]
当i为偶数时,由欧拉函数的性质,phi[2i] = 2phi[i]
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int ret = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
return w ? -ret : ret;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template <typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template <typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template <typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 20000005;
int _, n, tot, phi[N], prime[N];
ll a[N];
bool vis[N];
void calc(){
phi[1] = 1;
for(int i = 2; i < N; i ++){
if(!vis[i]){
prime[++tot] = i;
phi[i] = i - 1;
}
for(int j = 1; j <= tot && i * prime[j] < N; j ++){
vis[i * prime[j]] = true;
if(i % prime[j] == 0){
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
for(int i = 1; i < N; i ++){
if(i & 1) a[i] = a[i - 1] + phi[i] / 2;
else a[i] = a[i - 1] + phi[i];
}
}
int main(){
calc();
for(_ = read(); _; _ --){
n = read();
printf("%lld\n", a[n]);
}
return 0;
}