Cod sursa(job #2022569)

Utilizator sfechisalin@yahoo.comSfechis Alin [email protected] Data 16 septembrie 2017 18:57:11
Problema Progresie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.41 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fin("progresie.in");
ofstream fout("progresie.out");

int GetGroup(int64_t x) {
    return (int)sqrt(x - 1.0) + 1;
}

pair<int64_t, int64_t> GetGroupLimits(int g) {
    return make_pair(1LL * g * (g - 1) + 1, 1LL * g * g);
}

int main() {
    int testCount;
    fin >> testCount;

    while (testCount--) {
        int N, R;
        fin >> N >> R;

        for (int startGroup = 1;; ++startGroup) {
            int64_t curr = GetGroupLimits(startGroup).first;
            int64_t freeMove = startGroup - 1;

            bool found = true;
            for (int step = 2; step <= N; ++step) {
                curr += R;
                int currGroup = GetGroup(curr);

                int64_t start, finish;
                tie(start, finish) = GetGroupLimits(currGroup);

                if (curr < start) {
                    if (start - curr <= freeMove) {
                        freeMove -= start - curr;
                        curr = start;
                    }
                    else {
                        found = false;
                        break;
                    }
                }

                freeMove = min(freeMove, finish - curr);
            }

            if (found) {
                fout << curr - 1LL * (N - 1) * R << '\n';
                break;
            }
        }
    }

    return 0;
}