Cod sursa(job #122677)

Utilizator Adriana_SAdriana Sperlea Adriana_S Data 13 ianuarie 2008 13:50:47
Problema Puteri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <stdio.h>
#include <string.h>
#include <bitset>

using namespace std;

const int N_MAX = 100010;

struct putere {
	char a, b, c;
} v[N_MAX];

int nr[65][65][65];

char step[65][65][65], mod[130][130];

int kkt[] = {2,3,5,6,7,10,11,13,14,15,17,19,21,22,23,26,29,30,31,33,34,35,37,38,39,41,42,43,46,47,51,53,55,57,58,59,61,62,65,66,67,69,70,71,73,74,77,78,79,
82,83,85,86,87,89,91,93,94,95,97,101,102,103,105,106,107,109,110,111,113,114,115,118,119,122,123,127};

int kkt2[] = {0,0,0,2,0,2,0,0,2,2,0,0,2,2,0,2,0,3,0,2,2,2,0,2,2,0,3,0,2,0,2,0,2,2,2,0,0,2,2,3,0,2,3,0,0,2,2,3,0,2,0,2,2,2,0,2,2,2,2,0,0,3,0,3,2,0,0,3,2,0,3,
2,2,2,2,2,0};

int is[130];

int main()
{
	freopen("puteri.in", "r", stdin);
#ifndef _SCREEN_
	freopen("puteri.out", "w", stdout);
#endif

	int N, i, j;
	scanf("%d\n", &N);
	for (i = 1; i <= N; i ++) scanf("%d %d %d\n", &v[i].a, &v[i].b, &v[i].c);

	for (i = 1; i <= 128; i ++) {
		for (j = 1; j <= 128; j ++) {
			mod[i][j] = j % i;
		}
	}

	int nrdiv, x, y, z, stp = 1, p, a, b, c;
	long long fin = 0, rez;

	for (p = 0; p < 77; p ++) {
		nrdiv = kkt2[p];
		i = kkt[p];

		if (nrdiv == 0) nrdiv ++;
	
		for (j = 1; j <= N; j ++) {
			x = mod[i][v[j].a], y = mod[i][v[j].b], z = mod[i][v[j].c];
			if (step[x][y][z] < stp) {
				step[x][y][z] = stp;
				nr[x][y][z] = 1;
			} else nr[x][y][z] ++;
		}

		rez = 0;
		for (j = 1; j <= N; j ++) {
			x = mod[i][v[j].a], y = mod[i][v[j].b], z = mod[i][v[j].c];
			a = mod[i][(i - x)], b = mod[i][(i - y)], c = mod[i][(i - z)];

			if (a <= 64 && b <= 64 && c <= 64) {
	
				if  (nr[x][y][z]) {
	
					if (mod[i][(2 * x)] == 0 && mod[i][(2 * y)] == 0 && mod[i][(2 * z)] == 0) {
						rez += (long long) nr[x][y][z] * (nr[x][y][z] - 1) / 2;
					} else {
						if (nr[a][b][c]) {
							rez += (long long) nr[x][y][z] * nr[a][b][c];
							nr[a][b][c] = 0;
						}
					}
		
					nr[x][y][z] = 0;
				}
			}	
		}

		if (mod[2][nrdiv] == 1) fin += rez;
		else fin -= rez;

		stp ++;
	}

	printf("%lld\n", fin);

	return 0;
}