Monday, August 1, 2016

 lightoj-1008-Fibsieve`s Fantabulous Birthday

Suggestion: Don's use or see any source code before you have tried enough.

#include<stdio.h>
#include<math.h>
#define lli long long int
int main()
{
    long long int test,i,mid,low,high,k,f,m;
    double n,l;
    scanf("%lld",&test);

    for(i=1; i<=test; i++)
    {
        scanf("%lf",&n);
        l=floor(sqrt(n));
        f = ceil(sqrt(n));
        if(n==1)
        {
            printf("Case %lld: 1 1\n",i);
        }
        else if(f==l)
        {
            if(f%2==0)
            {
                printf("Case %lld: %lld 1\n",i,f);
            }
            else
            {
                printf("Case %lld: 1 %lld\n",i,f);
            }
        }
        else
        {

            k=l+1;
            m=l;
            if(m%2==0)
            {
                low=(l*l)+1;
                high=(k*k);
                mid=(low+high)/2;
                if(n==mid)
                {
                    printf("Case %lld: %lld %lld\n",i,k,k);
                }
                else if(n<mid)
                {
                    printf("Case %lld: %lld %lld\n",i,k,llabs(low-n)+1);
                }
                else
                {
                    printf("Case %lld: %lld %lld\n",i,llabs(high-n)+1,k);
                }
            }
            else
            {
                low=(l*l)+1;
                high=(k*k);
                mid=(low+high)/2;
                if(n==mid)
                {
                    printf("Case %lld: %lld %lld\n",i,k,k);
                }
                else if(n>mid)
                {
                    printf("Case %lld: %lld %lld\n",i,k,llabs(high-n)+1);
                }
                else
                {
                    printf("Case %lld: %lld %lld\n",i,llabs(low-n)+1,k);
                }
            }
        }
    }
    return 0;
}

No comments:

Post a Comment