题目链接
题意:
对于一个n行m列的方阵 第i行第j列的值是gcd(i, j) * 2 - 1 求方阵和 n, m <= 1e5
这道题很妙了
f[x] 为满足 gcd(i, j) = x 的数对(i, j)的个数
g[x] 为i,j因数包括x的数对(i, j)的个数
显然g[x] = ( n / x ) * ( m / x )
而f[x] + f[2x] + f[3x] + … = g[x]
所以f[x] = g[x] - f[2x] - f[3x] - …
然后从n到1计算f即可
复杂度O(n ln n)
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 1e5 + 5;
long long f[N], ans;
int n, m;
int main()
{
scanf("%d%d", &n, &m);
if(n > m) swap(n, m);
for(int i = n; i >= 1; --i){
f[i] = 1ll * (n / i) * (m / i);
for(int j = i << 1; j <= n; j += i) f[i] -= f[j];
ans += (f[i] * ((i << 1) - 1));
}
printf("%lld", ans);
return 0;
}