Pagini recente » Cod sursa (job #22971) | Cod sursa (job #1291462) | Cod sursa (job #2818431) | Cod sursa (job #2386685) | Cod sursa (job #236385)
Cod sursa(job #236385)
#include <cstdio>
#include <math.h>
#include <stdlib.h>
const unsigned NMAX = 1005, KEYLENGTH = 16;
int SALT = 2;
struct hash_el
{ double v; unsigned int nr; };
inline int hk(double x)
{
//int mask = ~(~0 << KEYLENGTH);
const double A = 0.618;
double t = A * x;
const unsigned int M = 1 << KEYLENGTH;
return floor( (t - floor(t)) * (M - 1) ) + 1;
}
bool add(hash_el* ls, double x)
{
unsigned int m = ls -> nr, i;
bool ok = false, added = false;
for (i = 1; i <= m; ++i) {
if (ls[i].v == x) {
added = true;
++ls[i].nr;
if (ls[i].nr == 2) ok = true;
break;
}
}
if (!added) {
i = ++(ls[0].nr);
ls[i].nr = 1;
ls[i].v = x;
}
if (ok && !(ls -> v)) {
ls -> v = 1;
return 1;
}
else return 0;
}
hash_el hash[(1 << KEYLENGTH) + 1][12] = {}, *hl[(1 << KEYLENGTH) + 5] = {};
int main()
{
unsigned int rasp = 0;
int N, x[NMAX] = {}, y[NMAX] = {};
int ii, jj, key, hllen = 0, k, m;
double panta, nr, nu;
hash_el **i, *j;
freopen("trapez.in", "r", stdin);
freopen("trapez.out", "w", stdout);
scanf("%d", &N);
for (ii = 0; ii != N; ++ii)
scanf("%d %d\n", &x[ii], &y[ii]);
for (ii = 0; ii != N; ++ii)
for (jj = ii + 1; jj != N; ++jj) {
nr = y[jj] - y[ii];
nu = x[jj] - x[ii];
if (!nu) {
key = 0;
panta = 2500000000;
}
else {
panta = nr / nu;
key = hk(panta);
}
if ( add(hash[key], panta) ) hl[hllen++] = hash[key];
}
for (i = hl; *i; ++i) {
j = *i;
m = j -> nr;
for (k = 1; k <= m; ++k)
rasp += j[k].nr * (j[k].nr - 1) / 2;
}
printf("%u\n", rasp);
fclose(stdin);
fclose(stdout);
return 0;
}