Pagini recente » Cod sursa (job #360652) | Cod sursa (job #2722924) | Cod sursa (job #2297865) | Cod sursa (job #1379322) | Cod sursa (job #1184360)
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
long long n,i,j,r,T;
long long x,y,z,t,i1,m,p,q;
int k;
bool ok;
int main()
{
freopen("progresie.in","r",stdin);
freopen("progresie.out","w",stdout);
scanf("%lld",&T);
while (T--)
{
scanf("%lld %lld",&n,&r);
for (i=1; ;++i)
{
p=(i*(i-1)+1)*1LL;
q=(i*i)*1LL;
m=i*1LL-1;
ok=1;
for (j=1;j<n;++j)
{
x=(p+(j*r*1LL))*1LL;
y=(x+m)*1LL;
k=(sqrt((x)));
if (k*k==x) --k;
--k;
z=(1LL*k*(k-1)+1);
t=k*k*1LL;
if (y<z)
{
ok=0;
break;
}
if (x<z)
{
p=p+(z-x);
m=m+(x-z);
}
if (y>t) m=m+(t-y);
}
if (ok)
{
printf("%lld\n",p);
break;
}
}
}
}