Cod sursa(job #1967708)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 16 aprilie 2017 23:20:59
Problema Progresie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.23 kb
#include <fstream>
//#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>

using namespace std;

ifstream cin("progresie.in");
ofstream cout("progresie.out");

int main() {
    int tests;
    cin >> tests;
    for (int test = 1; test <= tests; test++) {
        int n, r;
        cin >> n >> r;
        for (int i = 1; ; i++) {
            long long value = 1LL * i * (i - 1) + 1, limit = i - 1;
            bool ok = true;
            for (int j = 1; j < n && ok; j++) {
                value += r;
                int root = sqrt(value - 1) + 1;
                long long first = 1LL * root * (root - 1) + 1, last = 1LL * root * root;
                if (value < first) {
                    if (limit < first - value) {
                        ok = false;
                        break;
                    }
                    limit -= (first - value);
                    value = first;
                }
                if (limit > last - value)
                    limit = last - value;
            }
            if (ok) {
                cout << value - 1LL * (n - 1) * r << "\n";
                break;
            }
        }
    }
    return 0;
}