CodeForces 552 D.Vanya and Triangles(组合数学)
2018-01-10 11:07
513 查看
Description
给出二维平面上n个整点的坐标,问可以形成面积非零的三角形个数,两个三角形视为不同当且仅当其顶点集不同
Input
第一行一整数n表示点数,之后n行每行输入两个整数xi,yi表示第i个点的坐标,保证没有重点(1≤n≤2000,−100≤xi,yi≤100)
Output
输出可以形成的面积非零三角形数量
Sample Input
4
0 0
1 1
2 0
2 2
Sample Output
3
Solution
枚举三角形一个点,用map维护其他点到该点的斜率(最简形式),对于每个斜率取两个点即为不合法方案,统计所有不合法方案,从总方案数C3n中减去即可
Code
给出二维平面上n个整点的坐标,问可以形成面积非零的三角形个数,两个三角形视为不同当且仅当其顶点集不同
Input
第一行一整数n表示点数,之后n行每行输入两个整数xi,yi表示第i个点的坐标,保证没有重点(1≤n≤2000,−100≤xi,yi≤100)
Output
输出可以形成的面积非零三角形数量
Sample Input
4
0 0
1 1
2 0
2 2
Sample Output
3
Solution
枚举三角形一个点,用map维护其他点到该点的斜率(最简形式),对于每个斜率取两个点即为不合法方案,统计所有不合法方案,从总方案数C3n中减去即可
Code
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; typedef long long ll; typedef pair<int,int>P; const int INF=0x3f3f3f3f,maxn=2001; map<P,int>m; map<P,int>::iterator it; ll C(int n,int m) { if(m==2)return (ll)n*(n-1)/2; if(m==3)return (ll)n*(n-1)*(n-2)/6; } int gcd(int a,int b) { return b?gcd(b,a%b):a; } void deal(int x,int y) { if(x==0&&y==0)return ; if(x==0)m[P(0,1)]++; else if(y==0)m[P(1,0)]++; else { int g=gcd(abs(x),abs(y)); if(x<0)x=-x,y=-y; x/=g,y/=g; m[P(x,y)]++; } } int n,x[maxn],y[maxn]; int main() { int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]); ll ans=0; for(int i=1;i<=n;i++) { m.clear(); for(int j=1;j<=n;j++)deal(x[j]-x[i],y[j]-y[i]); for(it=m.begin();it!=m.end();it++) { P p=it->first; //printf("%d %d %d\n",p.first,p.second,it->second); ans+=C(it->second,2); } } ans=C(n,3)-ans/3; printf("%I64d\n",ans); } return 0; }
相关文章推荐
- 【25.33%】【codeforces 552D】Vanya and Triangles
- CodeForces 552D-Vanya and Triangles【计算整数三点能否组成三角形】
- CodeForces 552D — Vanya and Triangles 暴力枚举
- Codeforces 552D. Vanya and Triangles【向量判三点共线】
- Codeforces 552B - Vanya and Books(数学)
- codeforces 552 E. Vanya and Brackets
- Codeforces 560E Gerald and Giant Chess 组合数学+DP
- codeforces 914C. Travelling Salesman and Special Numbers (DP + 组合数学)
- 【33.33%】【codeforces 552B】Vanya and Books
- Codeforces Round #419 (Div. 2) (Codeforces 815B) D. Karen and Test 组合数学
- CodeForces 785D Anton and School - 2 组合数学
- Codeforces 559C Gerald and Giant Chess(组合数学+DP)
- codeforces(559C)--C. Gerald and Giant Chess(组合数学)
- Codeforces-785D-Anton and School - 2(组合数学,范德蒙恒等式)
- codeforces(559C)--C. Gerald and Giant Chess(组合数学)
- CodeForces 553 B.Kyoya and Permutation(组合数学)
- CodeForces 552C:Vanya and Scales【技巧】
- Codeforces 560 E. Gerald and Giant Chess (dp,组合数学)
- CodeForces 374 B.Inna and Nine(组合数学)
- CodeForces-552B - Vanya and Books