数三角形 (组合数学+分类讨论)

数三角形

[Link](信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn))

题意

给定一个 n × m n\times m n×m的网格,请计算三点都在格点上的三角形共有多少个。

题解

首先我们先将长度都加一变成格点数,发现直接去统计合法的方案不是很方便,我们可以容斥一下总方案-不合法方案,一共有 n × m n\times m n×m个点,从里面选三个点构成三角形,总方案为 C n × m 3 C_{n\times m}^{3} Cn×m3

不合法的方案分为四类:

  1. x x x轴平行: n × C m 3 n\times C_{m}^{3} n×Cm3 (一共 n n n行选一行,从 m m m列中选三列)

  2. y y y轴平行: m × C n 3 m\times C_{n}^{3} m×Cn3

  3. 三点在某一条斜率为 k k k(正数)*的线段上上,假设左下角和右上角分别为 A , B A,B A,B,且 A , B A,B A,B横坐标之差为 j j j,纵坐标之差为 i i i,这样的线段一共有 ( n − i ) ∗ ( m − j ) (n-i)*(m-j) (ni)(mj)条,然后线段 A , B A,B A,B之间的点 C C C一共有 g c d ( i , j ) − 1 gcd(i,j)-1 gcd(i,j)1种情况。

    为什么有 g c d ( i , j ) − 1 gcd(i,j)-1 gcd(i,j)1种情况?

    首先考虑特列从点 ( 0 , 0 ) (0,0) (0,0)到点 ( n , m ) (n,m) (n,m),所有经过的点都应该在 y = m n x y=\frac{m}{n}x y=nmx上,因为要经过格点,所以 x , y x,y x,y应均为整数,若要 y y y为整数则 m n x \frac{m}{n}x nmx必是整数,即 n ∣ m x n\mid mx nmx, 相当于 x x x要包含 n 除 去 g c d ( n , m ) n除去gcd(n,m) ngcd(n,m)以外的 n n n的约数才可以,所以 x x x只能是

    n ( n , m ) , 2 n ( n , m ) , 3 n ( n , m ) , . . . , ( n , m ) n ( n , m ) \frac{n}{(n,m)},2\frac{n}{(n,m)},3\frac{n}{(n,m)},...,(n,m)\frac{n}{(n,m)} (n,m)n,2(n,m)n,3(n,m)n,...,(n,m)(n,m)n,所以一共有 g c d ( n , m ) gcd(n,m) gcd(n,m)个,我们再除去右上角的格点,即一共有 g c d ( n , m ) − 1 gcd(n,m)-1 gcd(n,m)1个情况,等价到线段情况即可。

​ 因此这种不合法方案有 ( n − i ) ∗ ( m − j ) ∗ ( g c d ( i , j ) − 1 ) (n-i)*(m-j)*(gcd(i,j)-1) (ni)(mj)(gcd(i,j)1)

  1. 三点在某一条斜率为 k ( 负 数 ) k(负数) k()与情况三同理,方案数一样

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1010, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a[N];
int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}
LL C(int a, int b) {
    return (LL)a * (a - 1) * (a - 2) / 6;
}
int fact[N], infact[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> m;
    n ++, m ++;
    LL res = 0;
    res = (LL)C(n * m, 3)  - (LL)n * C(m, 3) - (LL)m * C(n, 3);
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
            res -= 2ll * (gcd(i, j) - 1) * (n - i) * (m - j);
    cout << res << endl;
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值