Cod sursa(job #1057079)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 14 decembrie 2013 14:17:42
Problema Progresie Scor 0
Compilator cpp Status done
Runda ONIS 2014, Runda 1 Marime 0.8 kb
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

int t, n, pas, m, pmm;
long long s;

long long verifx(long long x)
{
    int sol=(int)sqrt(x);

    return max(0LL, 1LL*sol*(sol+1)+1-x);
}

long long verif(long long s)
{
    pas++;
    long long ss=0;
    for(int i=0; i<n; ++i)
    {
        ss=verifx(s+1LL*m*i));
        if(ss>0)
            return ss;
    }
    return ss;
}

int main()
{
    freopen("progresie.in", "r", stdin);
    freopen("progresie.out", "w", stdout);

    scanf("%d", &t);
    long long x;
    while(t--)
    {
        scanf("%d%d", &n, &m);
        s=1LL*m;
        while(1)
        {
            x=verif(s);
            if(x==0)
                break;
            s+=x;
        }
        printf("%d\n", pas);
        printf("%lld\n", s);
    }
    return 0;
}