1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

void print_array_of_ints(int* array, int size) {
	int i;
	for (i = 0; i < size; i++) {
		printf("%d ", array[i]);
	}
	printf("\n");
}

void print_matrix_of_ints(int** array, int n, int m) {
	int i, j;
	for (i = 0; i < n; i++) {
		for (j = 0; i < m; j++) {
			printf("%d ", array[i][j]);
		}
		printf("\n");
	}
}

void print_string_list(char** array, int size) {
	int i;
	for (i = 0; i < size; i++) {
		printf("%s\n", array[i]);
	}
}

typedef struct {
	int size;
	int start;
	int end;
	char *data;
} buffer;

void print_buffer(buffer *buff) {
	puts("Buffer Data");
	printf("buff->start: %d\n", buff->start);
	printf("buff->end: %d\n", buff->end);
	int i;
	for (i = buff->start; i < buff->end; i++) {
		printf("%c", buff->data[i]);
		//printf("%d ", buff->data[i]);
	}
	puts("\n");
}

buffer* create_buffer(int size) {
	buffer *buff = (buffer*) malloc(sizeof (buffer));
	buff->size = size;
	buff->start = 0;
	buff->end = 0;
	buff->data = (char *) malloc((size + 1) * sizeof (char));
	buff->data[size] = '\0';
	return buff;
}

void rewind_buffer(buffer *buff) {

	int p = 0;
	int i;
	for (i = buff->start; i < buff->end; i++) {
		buff->data[p++] = buff->data[i];
	}
	buff->start = 0;
	buff->end = p;
}

void clear_buffer(buffer *buff) {
	buff->size = 0;
	buff->end = 0;
}

void destroy_buffer(buffer *buff) {
	free(buff->data);
	free(buff);
}

int read_buffer_fgets(buffer *buff) {
	char *result = fgets(buff->data, buff->size, stdin);
	if (result != NULL) {
		if (feof(stdin)) {
			int n = strlen(buff->data);
			if (buff->data[n - 1] == '\n') {
				buff->data[n - 1] = '\0';
				buff->end = n - 1;
			} else {
				buff->end = n;
			}
			return n;
		} else {
			buff->end = buff->size;
			return buff->size;
		}
	} else {
		return -1;
	}
}

int read_buffer_fread(buffer *buff) {
	int r_size = buff->size - buff->start;
	int n = fread(&(buff->data[buff->start]), sizeof (char), r_size, stdin);
	buff->end = buff->start + n;
	if (n < r_size) {
		buff->data[buff->start + n] = '\0';
	}
	return n;
}

void read_line_as_ints(int* values, int token_count, buffer *buff) {
	char *line = &(buff->data[buff->start]);
	int i, tc = 0;
	char c = NULL;
	for (i = buff->start; i < buff->end; i++) {
		c = buff->data[i];
		if (c == ' ' || c == '\0' || c == '\n') {
			buff->data[i] = '\0';
			values[tc++] = atoi(line);
			if (tc == token_count) {
				break;
			}
			line = &(buff->data[i + 1]);
		} /*else if (*c > 47 && *c < 58) {}*/
	}
	while ((c == ' ' || c == '\0' || c == '\n') && (i < buff->end)) {
		c = buff->data[++i];
	}
	if (i == buff->end) {
		clear_buffer(buff);
	} else {
		buff->start = i;
	}
	return;
}

int read_line(char* buffer, int* buff_size);

int read_line(char* buffer, int* buff_size);

#define SPACE_CHAR 32

#define FIRST_LINE_TOKEN_SIZE 2
#define FIRST_LINE_TOKEN_COUNT 1


#define BUFFER_SIZE 1024

int generate_fibbonaci_table(int * table, int max_value) {
	table[0] = 0;
	table[1] = 1;
	int i = 2;
	int r = 1;
	while (r < max_value) {
		table[i] = table[i - 2] + table[i - 1];
		r = table[i];
		i++;
	}
	return i ;
}

int fib_table[50];
int fib_t_size = 0;

char is_in_fib_table(int value) {
	if (value == 0 || value == 1 || value == 2) {
		return 'T';
	}
	int i, fvalue;
	for (i = fib_t_size; i > 1; i--) {
		fvalue = fib_table[i];
		if (fvalue == value) {
			return 'T';
		} else if (fvalue < value) {
			return 'N';
		}
	}
	return 'N';
}

int main() {
	buffer *buff = create_buffer(BUFFER_SIZE);
	int r_size = read_buffer_fread(buff);

	int first_line_data[FIRST_LINE_TOKEN_COUNT];
	read_line_as_ints(first_line_data, FIRST_LINE_TOKEN_COUNT, buff);

	int data[first_line_data[0]];
	read_line_as_ints(data, first_line_data[0], buff);

	fib_t_size = generate_fibbonaci_table(fib_table, 1000000000);
	//print_array_of_ints(fib_table, fib_t_size);
	int i;
	for (i = 0; i < first_line_data[0]; i++) {
		int v = data[i];
		if (is_in_fib_table(v) == 'T') {
			puts("TAK");
		} else {
			int j;
			int fval;
			//od 3 bo wczesniejsze wartosci beda OK w is_in_fib_table
			for (j = 3; j < fib_t_size; j++) {
				fval = fib_table[j];
				if (fval > v) {
					puts("NIE");
					break;
				} else if ((v % fval == 0) && is_in_fib_table(v / fval) == 'T') {
					puts("TAK");
					break;
				}
			}
		}
	}

	if (r_size > 0) {
		//print_buffer(buff);
		//TODO needs read more data
	}
	return 0;
}