Cod sursa(job #1060906)

Utilizator Teodor94Teodor Plop Teodor94 Data 18 decembrie 2013 21:28:07
Problema Progresie Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 1 kb
#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 );
}