Cod sursa(job #2341635)

Utilizator AxellApostolescu Alexandru Axell Data 12 februarie 2019 03:30:55
Problema Fractii Scor 20
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.94 kb
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char *set;

// Prezenta bitului i in set marcheaza faptul ca 2 * i + 1 este prim
// 0 inseamna ca valoarea este in set, in timp ce 1 marcheaza lipsa
void remove_from_set(set s, int val) {
	int byte = (val >> 3), bit = (val & 7);
	s[byte] |= (1 << bit);
}

int is_in_set(set s, int val) {
	int byte = (val >> 3), bit = (val & 7);
	if (s[byte] & (1 << bit)) {
		return 0;
	}
	return 1;
}

set declare_set(int n) {
	set s = calloc((n + 15) / 16, sizeof(char));
	if (s == NULL) {
		printf("Nu s-a putut aloca memorie\n");
		return NULL;
	}
	return s;
}

int counter(set s, int n) {
	int i, ct = 1;
	for (i = 3 ; i <= n ; i += 2) {
		if (is_in_set(s, i / 2)) {
			ct++;
		}
	}
	return ct;
}

// Contorul incepe de la 1, marcand prezenta lui 2 in set
void ciur(set s, int n) {
	int i, j;
	for (i = 1 ; ((i * i) << 1) + (i << 1) <= n ; ++i) {
		if (is_in_set(s, i)) {
			for (j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n ; j += (i << 1) + 1) {
				remove_from_set(s, j);
			}
		}
	}
}

int indicator(set s, int n);
int fractii_ireductibile(set s, int n);

int main() {
	FILE *in, *out;
	if (((in = fopen("fractii.in", "rt")) == NULL)) {
		printf("Nu am putut deschide fisierul de input!");
		return -1;
	}
	if (((out = fopen("fractii.out", "wt")) == NULL)) {
		printf("Nu am putut deschide fisierul de output!");
		return -2;
	}
	int n;
	fscanf(in, "%d", &n);
	set s = declare_set(n);
	ciur(s, n);
	fprintf(out, "%d\n", fractii_ireductibile(s, n));
	// Freeing the memory
	free(s);
	fclose(in);
	fclose(out);
	return 0;
}

int indicator(set s, int n) {
	int i, contor = n;
	if (!(contor & 1)) {
		contor /= 2;
	}
	for (i = 1 ; ((i << 1) + 1) <= n ; ++i) {
		if (is_in_set(s, i) && (n % ((i << 1) + 1) == 0)) {
			contor = contor / ((i << 1) + 1) * (i << 1);
		}
	}
	return contor;
}

int fractii_ireductibile(set s, int n) {
	int i, sum = 0;
	for (i = 1 ; i <= n ; ++i) {
		sum += indicator(s, i);
	}
	sum = sum * 2 - 1;
	return sum;
}