Friday, 15 January 2016

UVa 11489 - Integer Game

The trick here is to count the numbers%3.
Let the cnt of elements of array  with (num%3==0) be x.
Let the cnt of elements of array  with (num%3==1) be y.
Let the cnt of elements of array  with (num%3==2) be z.

Then the question deals with the rem number should be divisible by 3 after removing an element that means that sum%3==0 after removing an element..
So if initial sum%3==0 then a number with %3==0 can only be removed at all attempts.
If initial sum%3==(1 or 2) then a number with %3==(1 or 2) respectively can be removed at first attempt (if available or S loses) and then %3==0 numbers can only be removed in successive attempts.
So the last person to remove %3==0 element wins the game...

//\\__ hr1212 __//\\

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;

#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007

int n,m;

int main(){
    int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0,tt=1;
    ll ans=0;string p;

    si(t);
    while(t--){
        printf("Case %d: ",tt++);
        cin>>p;
        x=0;y=0;z=0;ans=0;
        n=p.size();
        f(i,0,p.size()){
            l=p[i]-'0';
            if(l%3==0)
                x++;
            else if(l%3==1)
                y++;
            else
                z++;
            ans=(ans+l)%3;
        }
        if(ans==0){
            if(x%2==0)
                r=1;
            else
                r=0;
        }
        else if(ans==1){
            if(n==1)
                r=0;
            else if(y==0)
                r=1;
            else{
                if((x+1)%2==0)
                    r=1;
                else
                    r=0;
            }
        }
        else{
            if(n==1)
                r=0;
            else if(z==0)
                r=1;
            else{
                if((x+1)%2==0)
                    r=1;
                else
                    r=0;
            }
        }
        if(r)
            printf("T");
        else
            printf("S");
        nl;
    }

    return 0;
}

No comments:

Post a Comment