Cod sursa(job #1439340)

Utilizator liisLIIS-Horia-Vlad-Denis liis Data 22 mai 2015 10:23:30
Problema Progresie Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 0.91 kb
// progresie.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"

#include <fstream>
#include <math.h>

using namespace std;
ifstream f("progresie.in");
ofstream g("progresie.out");


long long t, n, r, ok, i, sav, inceput = 1, lg = 1;

int bun(long long  x)
{
	long long k = sqrt(x);
	if (k*k < x) k++;
	if (x >= k * (k - 1) + 1 && x <= k*k)
		return 1;
	return 0;
}

//int _tmain(int argc, _TCHAR* argv[])
int main()
{
	f >> t;
	for (i = 1; i <= t; i++)
	{
		f >> n >> r;
		ok = 0; inceput = 1;
		lg = 1;
		while (ok == 0)
		{
			long long  j = inceput;
				ok = 1;
				for (int j2 = 2; j2 <= n; j2++)
				{
					if (bun(j+(j2-1)*r) == 0)
					{
						ok = 0;
						break;
					}
				}
				if (ok == 1)
				{
					sav = j;
					break;
				}
			inceput += lg * 2;
			lg++;
		}
		g << sav << '\n';
	}


	f.close();
	g.close();
	return 0;
}