您的位置:首页 > 其它

POJ_1809_Regetni(组合数学)

2013-07-14 22:57 302 查看
D - Regetni
Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice POJ
1809

Description

Background

Hello Earthling. We're from the planet Regetni and need your help to make lots of money. Maybe we'll even give you some of it.

You see, the problem is that in our world, everything is about integers. It's even enforced by law. No other numbers are allowed for anything. That said, it shouldn't surprise you that we use integer coordinate systems to plan our cities. So far only axis-aligned
rectangular plots of land have been sold, but our professor Elgnairt recently had the revolutionary idea to sell triangular plots, too. We believe that the high society will love this concept and it'll make us rich.

Unfortunately the professor patented his idea and thus we can't just do it. We need his permission and since he's a true scientist, he won't give it to us before we solve some damn riddle. Here's where you come in,because we heard that you're a genius.

Problem

The professor's riddle goes like this: Given some possible corners for the triangles, determine how many triangles with integral size can be built with them. Degenerated triangles with empty area (i.e. lines) have to be counted, too, since 0 is an integer.
To be more precise, count the number of triangles which have as corners three different points from the input set of points. All points in a scenario will be distinct, i.e. there won't be duplicates. Here are some examples:



Example a) shows a triangle with integral area (namely 3), b) shows one with non-integral size, c) shows a degenerated triangle with empty area (i.e. zero, so count it!), d) shows four points of which you can choose any three to build an integral area triangle
and e) shows four points where you can't build any integral area triangles at all.

Hint: The area A of a triangle with corners (x1, y1), (x2, y2) and (x3, y3) can be computed like this:

A=|x1y2 - y1x2 + x2y3 - y2x3 + x3y1 - y3x1|/2

Try to make clever use of this formula.

Input

The first line contains the number of scenarios. For each scenario, there is one line containing first the number N of distinct points in that scenario (0 <= N <= 10000) and after that N pairs of integers, each pair describing one point (xi, yi) with -100000
<= xi, yi <= 100000. All these numbers are separated by single blanks.

Output

Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the number of triangles with integral area whose three distinct corners are among the points
given. Terminate the output for each scenario with a blank line.

Sample Input

6
3 0 0 2 0 1 -3
3 0 0 2 1 1 -3
3 0 0 2 2 3 3
4 0 0 2 0 0 2 2 2
4 0 0 1 0 0 1 1 1
9 0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2


Sample Output

Scenario #1:
1

Scenario #2:
0

Scenario #3:
1

Scenario #4:
4

Scenario #5:
0

Scenario #6:
48


题型:组合数学

题意:

给出一堆坐标,任选3个,看|x1y2 - y1x2 + x2y3 - y2x3 + x3y1 - y3x1|/2 是不是个整数。问这样的额组合有多少种。

分析:

乍看木有思路,仔细想想后就觉得灰常简单了。。。

想求|x1y2 - y1x2 + x2y3 - y2x3 + x3y1 - y3x1|/2是不是个整数,只要求x1y2 - y1x2 + x2y3 - y2x3 + x3y1 - y3x1是个偶数即可。合并一下,就是

A=y1*(x3-x2) + y2*(x1-x3) + y3*(x2-x1)

若A为偶数,那么只有偶+偶+偶 和 奇+奇+偶 这两种情况。

那么首先我们可以先算出所有的情况,即C(n,3)

然后通过将奇偶数转化为0和1来减去不符合 偶+偶+偶 和 奇+奇+偶 的情况

总结一下有四个不满足的情况:

(0,0) (0,1) (1,0)

(0,0) (0,1) (1,1)

(0,0) (1,0) (1,1)

(0,1) (1,0) (1,1)

完美的O(n)复杂度,妙哉~

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
long long cnt[2][2],n,x,y;
int main(){
int t;
int flag=1;
scanf("%d",&t);
while(t--){
cnt[0][0]=cnt[1][0]=cnt[0][1]=cnt[1][1]=0;
scanf("%lld",&n);
for(int i=0;i<n;i++){
scanf("%lld%lld",&x,&y);
cnt[x&1][y&1]++;
}
long long ans=n*(n-1)*(n-2)/6;
ans-=cnt[0][0]*cnt[0][1]*cnt[1][0];
ans-=cnt[0][0]*cnt[0][1]*cnt[1][1];
ans-=cnt[0][0]*cnt[1][0]*cnt[1][1];
ans-=cnt[0][1]*cnt[1][0]*cnt[1][1];
printf("Scenario #%d:\n",flag);
flag++;
printf("%lld\n\n",ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: