AcWing 1310. 数三角形(组合数)

数三角形

简单容斥。
先算所有任取3点的方案数。
难的是求出不合法的方案数。

第一种是三点都在同一条横线。
第二是三点都在同一条竖线上。
第三种是在同一条斜线上。我们先求出斜率大于0的情况,小于的和大于0的一样。
我们可以来枚举这条斜线的宽和高。
gcd(i,j)-1求出这条斜线中间的点数

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#include<bitset>
#include<unordered_map>
using namespace std;
#define LL long long
#define eps (1e-9)
typedef unsigned long long ull;

const int maxn = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const int bas = 131;
const LL mod = 100003;

LL gcd(LL a, LL b)
{
	if (!b)return a;
	else return gcd(b, a % b);
}

LL cal(LL a)
{
    if (a < 3)return 0;
	return a * (a - 1) * (a - 2) / 6;
}
int main()
{
	int n, m;
	scanf("%d%d", &n, &m);

	n++, m++;
	LL res = cal(n * m);

	res -= n * cal(m);
	res -= m * cal(n);

	n--, m--;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			res -= 2*(gcd(i, j) - 1) * (n - i + 1) * (m - j + 1);
		}
	}
	printf("%lld", res);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值