Codeforces Round #308 (Div. 2) D. Vanya and Triangles

D. Vanya and Triangles
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane.

Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.

Output

In the first line print an integer — the number of triangles with the non-zero area among the painted points.

Sample test(s)
input
4
0 0
1 1
2 0
2 2
output
3
input
3
0 0
1 1
2 0
output
1
input
1
1 1
output
0
Note

Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0)(0, 0) - (2, 2) - (2, 0)(1, 1) - (2, 2) - (2, 0).

Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).

Note to the third sample test. A single point doesn't form a single triangle.

题目要求是求出n个点所能组成的三角形的个数,我们知道,若都能组成三角形,则总个数为c(n,3)个,现在目标就是求出不能组成三角形的个数,则

可以这样求,以一个点为起点,其它点与这个点的斜率保存在map中,则对于每个则有c(m,2)个不能组成三角形,因为重复算了三次,最后要除以3,

还要用long long 保存结果,保存斜率可以用分数a/b,这里a,b互质,分数可以用a*N+b一个整数保存起来,就没有误差了!

#define INF			9000000000000000000
#define EPS			(double)1e-9
#define mod			1000000007
#define PI			3.14159265358979
//*******************************************************************************/
#endif


#define N 2005
#define MOD 1000000007
int n;
struct node{
    int x,y;
    node(int xx,int yy){
        x = xx,y = yy;
    }
    node(){
        x = 0;y=0;
    }
};
node nodes[N];
map<int,int> mymaps;
map<int,int>::iterator it;
int get(int a,int b){//a/b
    if(a == 0) return 0;
    if(b == 0) return N * N + N;
    int sa = a>=0?1:-1,sb= b>=0?1:-1;
    sa = sa * sb;sb = 1;
    a = abs(a);b=abs(b);
    int g = gcd(a,b);
    a/=g;b/=g;
    return sa * a * N + sa * b;
}
int main()
{
    //printf("%d",get(-1,2));
    while (S(n) != EOF)
    {
        FI(n){
            S2(nodes[i].x,nodes[i].y);
        }
        long long ans = (long long)n*((long long)n-1)*((long long)n-2)/6,anst = 0;
        FI(n){
            mymaps.clear();
            FJ(n){
                if(i != j){
                    mymaps[get(nodes[j].y - nodes[i].y,nodes[j].x - nodes[i].x)]++;
                }
            }
            for(it = mymaps.begin();it!=mymaps.end();it++){
                int num = it->second;
                anst += num>=2? num*(num-1)/2:0;
            }
        }
        cout<<ans - anst/3<<endl;
    }
    return 0;
}


你可能感兴趣的:(Codeforces Round #308 (Div. 2) D. Vanya and Triangles)