Cod sursa(job #1326688)

Utilizator wGEORGEWGeorge Cioti wGEORGEW Data 25 ianuarie 2015 20:34:25
Problema Progresie Scor 100
Compilator cpp Status done
Runda Arhiva ICPC Marime 0.96 kb
#include <fstream>
#include <cmath>
using namespace std;
  
ifstream fin("progresie.in");
ofstream fout("progresie.out");
  
int T, N, R;
  
long long solve(int N, int R) {
  for (int i = 0; ; ++ i) {
    long long start = 1LL * i * (i + 1) + 1;
    long long margin = i;
    int isOK = 1;
  
    for (int it = 1; it < N; ++ it) {
      long long low = start + 1LL * it * R;
      long long high = low + margin;
      int j = (int)sqrt(low - 1);
      long long lowi = 1LL * j * (j + 1) + 1;
      long long 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() {
  fin >> T;
  for (int t = 1; t <= T; ++ t) {
    fin >> N >> R;
  
    fout << solve(N, R) << "\n";
  }
}