题目描述:
给了你一个n边形,现在需要你求它所有对角线得到的交点的个数。
题解:
还是排列组合的知识,我们可以发现对于两条对角线相交的点,其对应的就是n边形上的4个顶点,所以反过来想,就是四个顶点确定一个交点,所以四个顶点的选择就成了排列组合的问题了。
我们很容易写出n * (n - 1) * (n - 2) * (n - 3)
但是我们也注意到,这样的选择需要去除重复问题,也就是不考虑顺序的,所以需要再除以一个4! = 24
。
同时,对于上式有爆LL
的可能,所以我们可以把24分为2,3,4,其合理性在于,对于n,(n - 1),(n - 2),(n - 3),总会有数满足整除2,3,4,所以这样不但不会爆整数,同时也防止爆LL
AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ull unsigned long long //注意用ull因为数据过大还是可能爆ll
using namespace std;
int main()
{
ull n;
cin >> n;
ull ans = n * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4;
cout << ans << endl;
return 0;
}