传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2005
【题解】
题目要求$sum_{i=1}^nsum_{j=1}^m ((i,j) * 2 + 1)$
考虑容斥,$t_i$表示有公约数$i$的方案数,显然是$lfloor n/i floor lfloor m/i floor$。
$f_i$表示有最大公约数$i$的方案数,那么有$f_i = t_i - sum_{j=2}^{lfloor n/i floor} f_{ij}$
倒过来做显然就行了,复杂度是$O(nlogn)$。
# include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> // # include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const int M = 5e5 + 10; const int mod = 1e9+7; int n, m; ll f[M], ans; int main() { cin >> n >> m; if(n > m) swap(n, m); for (int i=n; i; --i) { f[i] = 1ll * (n/i) * (m/i); for (int j=i+i; j<=n; j+=i) f[i] -= f[j]; ans += f[i] * (i+i-1); } cout << ans; return 0; }