# bzoj3505: [Cqoi2014]数三角形

#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 1003
#define MAXP 170
int gcd(int x, int y)
{
while (y)
{
int t = x % y;
x = y;
y = t;
}
return x;
}
int n, m;
int a[MAXN][MAXN], ct[MAXN][MAXN];
long long f[MAXN][MAXN];
int main()
{
scanf("%d%d", &n, &m);
++n, ++m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
{
int g = gcd(i, j);
int x = i / g, y = j / g;
a[i][j] = ct[x][y]++;
}
for (int i = 1; i <= n; ++i)
f[i][1] = f[i - 1][1] + a[i][1];
for (int i = 1; i <= m; ++i)
f[1][i] = f[1][i - 1] + a[1][i];
for (int i = 2; i <= n; ++i)
for (int j = 2; j <= m; ++j)
f[i][j] = f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] + a[i][j];
int N = n * m;
long long ans = (long long)N * (N - 1) * (N - 2) / 6;
ans -= (long long)m * (m - 1) * (m - 2) / 6 * n + (long long)n * (n - 1) * (n - 2) / 6 * m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
ans -= f[n - i][j - 1] + f[n - i][m - j];
printf("%lld\n", ans);
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120