Description
给你一个 (n imes m) 的坐标轴。对于坐标轴的每一个正整数整点 ((x,y)) 其对答案产生的贡献为 (2k+1) ,其中 (k) 表示这个点与坐标原点连线,线段穿过了除端点外的 (k) 个点。求所有点的贡献和。
(1leq n,m leq 100000)
Solution
容易发现 (k=gcd(x,y)-1) ,故原式等于求 [egin{aligned}&sum_{i=1}^nsum_{j=1}^m(2(gcd(i,j)-1)+1)\=&2sum_{i=1}^nsum_{j=1}^mgcd(i,j)-nmend{aligned}]
记 (f(n,m)=sumlimits_{i=1}^nsumlimits_{j=1}^mgcd(i,j)) ,考虑如何求 (f(n,m)) [egin{aligned}Rightarrow&sum_{d=1}^{min{n,m}}dsum_{i=1}^{leftlfloorfrac{n}{d} ight floor}sum_{j=1}^{leftlfloorfrac{m}{d} ight floor}[gcd(i,j)=1]\=&sum_{d=1}^{min{n,m}}dsum_{i=1}^{leftlfloorfrac{n}{d} ight floor}sum_{j=1}^{leftlfloorfrac{m}{d} ight floor}sum_{kmid gcd(i,j)}mu(k)\=&sum_{d=1}^{min{n,m}}dsum_{k=1}^{minleft{leftlfloorfrac{n}{d} ight floor,leftlfloorfrac{m}{d} ight floor ight}}mu(k)leftlfloorfrac{n}{kd} ight floorleftlfloorfrac{m}{kd} ight floorend{aligned}]
令 (T=kd) ,枚举 (T) [egin{aligned}sum_{T=1}^{min{n,m}}leftlfloorfrac{n}{T} ight floorleftlfloorfrac{m}{T} ight floorsum_{dmid T}dcdotmuleft(frac{T}{d} ight)end{aligned}]
记后面那个狄利克雷卷积形式的式子为 (F(T)) ,显然这个是可以枚举因子在近似于 (O(nln n)) 的时限内预处理出来的。然后数论分块的复杂度为 (O(sqrt n)) ,对于 (t) 组询问...哦...没有 (t) 组询问...那我最后一步还搞个屁啊...
Code
//It is made by Awson on 2018.2.22
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('
'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 100000;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }
int n, m, mu[N+5];
LL F[N+5];
void get_F() {
int isprime[N+5], prime[N+5], tot = 0;
memset(isprime, 1, sizeof(isprime)); isprime[1] = 0, mu[1] = 1;
for (int i = 2; i <= N; i++) {
if (isprime[i]) mu[i] = -1, prime[++tot] = i;
for (int j = 1; j <= tot && i*prime[j] <= N; j++)
if (i%prime[j] != 0) isprime[i*prime[j]] = 0, mu[i*prime[j]] = -mu[i];
else {isprime[i*prime[j]] = 0, mu[i*prime[j]] = 0; break; }
}
for (int i = 1; i <= N; i++) for (int j = 1; j*i <= N; j++) F[i*j] += i*mu[j];
for (int i = 1; i <= N; i++) F[i] += F[i-1];
}
LL cal(int n, int m) {
if (n > m) Swap(n, m); LL ans = 0;
for (int i = 1, last; i <= n; i = last+1) {
last = Min(n/(n/i), m/(m/i));
ans += 1ll*(n/i)*(m/i)*(F[last]-F[i-1]);
}
return ans;
}
void work() {
read(n), read(m); get_F(); writeln(2ll*cal(n, m)-1ll*n*m);
}
int main() {
work(); return 0;
}