Cod sursa(job #1349676)

Utilizator RanKBrinduse Alexandru RanK Data 20 februarie 2015 13:16:21
Problema Progresie Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.69 kb

#define _CRT_SECURE_NO_WARNINGS

#include <stdlib.h>
#include <stdio.h>

#include <math.h>

#define IN_FILE_NAME "progresie.in"
#define OUT_FILE_NAME "progresie.out"

unsigned int CheckNr(unsigned int nr)
{
	unsigned int sqrtInt = (unsigned int)(sqrt(1.0 * nr));
	double sqrtDouble = sqrt(1.0 * nr);

	if(sqrtDouble == sqrtInt) return 0;
	if(nr > sqrtInt * (sqrtInt + 1) && nr < (sqrtInt + 1) * (sqrtInt + 1))
		return 0;

	return sqrtInt * (sqrtInt + 1) + 1 - nr;
}

int main()
{
	freopen(IN_FILE_NAME, "r", stdin);
	freopen(OUT_FILE_NAME, "w", stdout);

	unsigned int tests = 0, t;
	scanf("%d", &tests);

	for(t=0; t<tests; t++)
	{
		unsigned int globalMove = 0;
		bool found = false;
		unsigned int maxMove = 0;
		unsigned int n=0, r=0;
		scanf("%d %d", &n, &r);

		unsigned int i,j;
		for(i=1; i<=(n-1)*r; i++)
		{
			maxMove = 0;
			unsigned int start = 1+i*(i-1)+globalMove;
			unsigned int stop = i*i;
			for(j=start; j<=stop; j++)
			{				
				unsigned int k;
				found = true;
				for(k=1; k<n; k++)
				{					
					unsigned int nr = j+k*r;
					unsigned int sqrtInt = (unsigned int)(sqrt(1.0 * nr));
					double sqrtDouble = sqrt(1.0 * nr);
					unsigned int localMove;
					if(sqrtDouble == sqrtInt) localMove = 0;
					else if(nr > sqrtInt * (sqrtInt + 1) && nr < (sqrtInt + 1) * (sqrtInt + 1))
						localMove = 0;
					else
						localMove = sqrtInt * (sqrtInt + 1) + 1 - nr;

					if(localMove != 0) {found=false; break;}
				}
				if(found) {
					printf("%u\n", j);
					break;					
				}
			}
			if(found) break;
			//prunsigned intf("\n");
		}
		if(!found)
		{
			printf("%u\n", 1 + ((n-1)*r+1) * ((n-1)*r));
		}
	}	
	return 0;
}