Cod sursa(job #1057045)

Utilizator UPB_Bivol_Mincu_SanduDiana Mincu UPB_Bivol_Mincu_Sandu Data 14 decembrie 2013 14:13:01
Problema Progresie Scor 0
Compilator c Status done
Runda ONIS 2014, Runda 1 Marime 0.84 kb
#include <stdio.h>
#include <string.h>
#include <math.h>

int main() {
	FILE *f, *g;
	int ok;
	long i, j, t, n, r;
	long start, hm, skip, result, current, nr;
	char sir[10];

	f = fopen("progresie.in", "r");
	g = fopen("progresie.out", "w");

	fscanf(f,"%li",&t);
	for (i = 0; i < t; i++) {
		fscanf(f, "%li %li", &n, &r);
		
		start = 1;
		hm = 1;
		skip = 2;

		while (1) {
			ok = 1;

			for (j = 0; j < n; j++) {
				current = start + j * r;
				nr = (int)(sqrt(current) + 0.5);
				result = nr * nr;
				if (((result - nr) >= current) || (current > result && current < (result + nr + 1))) {
					ok = 0;
					break;
				}
			}

			if (ok == 0) {
				if (start == hm) {
					start += (hm + 1);
					skip += 2;
					hm++;
				} else {
					start++;
				}
			} else {
				fprintf(g, "%li\n", start);
				break;
			}
		}
	}

	fclose(f);
	fclose(g);

	return 0;
}