Pagini recente » Cod sursa (job #2129882) | Cod sursa (job #1574315) | Cod sursa (job #2166885) | Cod sursa (job #782834) | Cod sursa (job #1184355)
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,i,j,r,T;
long long x,y,z,t,i1,m,p,q,k;
bool ok;
int main()
{
freopen("progresie.in","r",stdin);
freopen("progresie.out","w",stdout);
scanf("%d",&T);
for (i1=1;i1<=T;i1++)
{
scanf("%d %d",&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=(long long)(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("%d\n",p);
break;
}
}
}
}