Cod sursa(job #1056572)

Utilizator cahemanCasian Patrascanu caheman Data 14 decembrie 2013 13:15:31
Problema Progresie Scor 0
Compilator cpp Status done
Runda ONIS 2014, Runda 1 Marime 0.91 kb
#include<cstdio>
#include<cmath>

using namespace std;

int main()
{
  freopen("progresie.in", "r", stdin);
  freopen("progresie.out", "w", stdout);
  int t, n, r, i, j, k, done, x, y, elem;
  scanf("%d", &t);
  for(i = 1; i <= t; ++ i)
  {
    scanf("%d%d", &n, &r);
    done = 0;
    for(k = 1; done < 1; ++ k)
    {
      x = sqrt(k);
      y = k - ((x * (x + 1)));
      if(y < 0)
      {
        y += x + x;
        -- x;
      }
      if(y <= x + 1 && y != 0)
      {
        elem = k;
        for(j = 1; j < n; ++ j)
        {
          elem += r;
          x = sqrt(elem);
          y = elem - ((x * x + x));
          if(y < 0)
          {
            y += x + x;
            -- x;
          }
          if(y > x + 1 || y == 0)
            break;
        }
        if(j == n)
        {
          printf("%d\n", k);
          done = 1;
        }
      }
    }
  }
  return 0;
}