UVa 10025 – The ? 1 ? 2 ? … ? n = k problem [solved]


/******************************************
          Mobarak Hosen Shakil
        ICE, Islamic University
     ID: mhiceiuk(all), 29698(LOJ)
   E-mail: mhiceiuk @ (Gmail/Yahoo/FB)
 Blog: https://iuconvergent.wordpress.com
*******************************************/

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long k, x, c, ans=0, d;
    int tn;
    scanf("%d", &tn);
    while(tn--)
    {
        scanf("%lld", &k);
        k=abs(k);

        if(k==0)
        {
            printf("3\n");
        }
        else {
            c= sqrt(k*2.0);
            x=c*(c+1)/2;

            if(x<k)
                c+=1;
            for(long long i=c; i<c+10;i++)
            {
                x=i*(i+1)/2;
                d=abs(x-k);
               /// cout<<x<< " "<<d<<endl;
                if(d%2==0)
                {
                    c=i;
                    break;
                }
            }
            printf("%lld\n", c);
        }
        if(tn!=0)
            printf("\n");
    }
    return 0;
}

A novice competitive programmer. Studying Bachelor of Science in Information and Communication Engineering, Islamic University, Kushtia-7003.

Tagged with:
Posted in UVa Problems

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow IU Convergent on WordPress.com
Community!
Views
  • 29,814 views