Pagini recente » Cod sursa (job #338285) | Cod sursa (job #1504520) | Cod sursa (job #2981090) | Cod sursa (job #290503) | Cod sursa (job #3146831)
#include <cmath>
#include <fstream>
using namespace std;
long long calculate_solution(const int n, const int r)
{
for (long long element = 1; ; ++element)
{
long long least_number = element * (element - 1) + 1, limit = element - 1;
auto found = true;
for (auto i = 1; i < n; ++i)
{
long long current_value = least_number + static_cast<long long>(i) * r;
const auto size = static_cast<long long>(sqrt(current_value - 1)) + 1;
if (const auto first = size * (size - 1) + 1; current_value < first)
{
if (limit < first - current_value)
{
found = false;
break;
}
least_number += first - current_value;
limit -= first - current_value;
current_value = first;
}
if (const auto last = size * size; limit > last - current_value)
{
limit = last - current_value;
}
}
if (found)
{
return least_number;
}
}
return -1;
}
int main()
{
ifstream input("progresie.in");
ofstream output("progresie.out");
int t;
input >> t;
for (auto i = 0; i < t; ++i)
{
int n, r;
input >> n >> r;
output << calculate_solution(n, r) << "\n";
}
return 0;
}