Pagini recente » Cod sursa (job #2456003) | Cod sursa (job #3251858) | Cod sursa (job #461685) | Statistici Filip Crisan (filicri) | Cod sursa (job #1060906)
#include <cstdio>
#include <cmath>
#define ll long long
ll start( int block ) {
return ( ll ) block * ( block - 1 ) + 1;
}
bool inside( ll x ) {
int block = sqrt( x );
if ( ( ll ) block * block != x )
++block;
ll left = start( block ), right = ( ll ) start( block ) + block - 1;
return ( left <= x && x <= right );
}
bool check( int block, int n, int r ) {
ll x = start( block );
int i = 0;
while ( i < n && inside( x ) ) {
x = ( ll ) x + r;
++i;
}
return ( i == n );
}
ll solve( int n, int r ) {
int block = 1;
while ( !check( block, n, r ) )
++block;
return start( block );
}
int main() {
FILE *fin, *fout;
fin = fopen( "progresie.in", "r" );
fout = fopen( "progresie.out", "w" );
int t;
fscanf( fin, "%d", &t );
while ( t ) {
int n, r;
fscanf( fin, "%d%d", &n, &r );
fprintf( fout, "%lld\n", solve( n, r ) );
--t;
}
fclose( fin );
fclose( fout );
}