Cod sursa(job #1713736)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 6 iunie 2016 13:54:05
Problema Progresie Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.08 kb
using namespace std;
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

ll solve(ll, ll);
ll getInt(ll);

int main()
{
	int t, n, r;
	ifstream fin("progresie.in");
	ofstream fout("progresie.out");

	for (fin >> t; t; --t)
	{
		fin >> n >> r;

		fout << solve(n, r) << '\n';
	}

	fin.close();
	fout.close();

    return 0;
}

ll solve(ll n, ll r)
{
	ll i, j, low, upp, d, start, nextEl, intv;
	bool ok;

	for (i = 0; ; ++i)
	{
		start = i * (i + 1) + 1;
		d = i;

		for (ok = true, j = 1; j < n && ok; ++j)
		{
			nextEl = start + j * r;
			intv = getInt(nextEl);

			low = intv * (intv + 1) + 1;
			upp = (intv + 1) * (intv + 1);

			if (nextEl >= low) d = min(d, upp - nextEl);
			else if (d < low - nextEl) ok = false;
			else
			{
				d -= low - nextEl;
				start += low - nextEl;
			}
		}

		if (ok) return start;
	}
}

ll getInt(ll x)
{
	return sqrt(x - 1);
}