Monday, August 1, 2016

lightoj-1020- A Childhood Game



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

It's a game theoretical basic problem.If you do not understand it please read first from Shafayater blog>game theory(1).
Source Code in C:

#include<stdio.h>
#include<string.h>
int main()
{
    long long int n,i,m,t;
    char str[10];
    scanf("%lld",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%lld ",&t);
        scanf("%s",str);

        m=strcmp(str,"Alice");
        if(m==0)
        {
            if(t==1||(t-1)%3==0)
            {
                printf("Case %lld: Bob\n",i);
            }
            else
                 printf("Case %lld: Alice\n",i);
        }else
        {
            if(t%3==0)
            {
                printf("Case %lld: Alice\n",i);
            }
            else
                 printf("Case %lld: Bob\n",i);
        }
    }
    return 0;
}


 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;
}