Cod sursa(job #1572459)

Utilizator tudormaximTudor Maxim tudormaxim Data 18 ianuarie 2016 22:16:38
Problema Progresie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

long long solve(int n, int r)
{
    int i, it, j;
    long long start, margin, isOK, low, high, lowi, highi;
    for(i=0; ; ++i)
    {
        start = 1LL * i * (i + 1) + 1;
        margin = i;
        isOK = 1;
        for(it=1; it<n; it++)
        {
            low = start + 1LL * it * r;
            high = low + margin;
            j = (int)sqrt(low - 1);
            lowi = 1LL * j * (j + 1) + 1;
            highi = 1LL * (j + 1) * (j + 1);
            if (high < lowi)
            {
                isOK = 0;
                break ;
            }

            if (low < lowi)
            {
                start += lowi - low;
                margin -= lowi - low;
            }
            else if (high > highi)
                margin -= high - highi;
        }

    if (isOK) return start;
    }
}

int main()
{
    ifstream fin("progresie.in");
    ofstream fout("progresie.out");
    ios_base::sync_with_stdio(false);
    int n, r, t;
    fin >> t;
    while(t--)
    {
        fin >> n >> r;
        fout << solve(n, r) << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}