Page 1 of 1

11489 - Integer Game

Posted: Fri Sep 19, 2008 1:09 pm
by aha2007
Anyone give me any hints:) Really cool Problem

Re: 11489 - Integer Game how to solve?

Posted: Fri Sep 19, 2008 6:10 pm
by andmej
There's an O(n) solution.

Invisible text follows:

First determine if the first player can remove a number.
If he can, then notice than from now on the only possible numbers than can be removed are 3, 6, or 9. Also notice that it's completely equivalent to delete any removable number, because you won't get any extra advantage if you choose 9 instead of 3 or whatever. The only thing that matters is that it must be divided evenly by 3.
From this, you can count the number of removable digits and determine the winner by the parity of the count.


Good luck!

Re: 11489 - Integer Game how to solve?

Posted: Sat Sep 20, 2008 12:58 pm
by peterkwan
I am getting WA all the time. Please help to give some critical inputs. Thanks.

Code: Select all

#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  for (int i=0; i<n; i++) {
    string s;
    cin >> s;
    int n2 = s.size();
    cout << "Case " << (i+1) << ": ";
 //   cout << s << " - ";

    if (n2==1)
      cout << "S" << endl;
    else if (n2==2)
      cout << "T" << endl;
    else {
      int sum = 0, n1 = 0;
      for (int j=0;j<n2;j++) {
        int c=(s[j]-'0');
        sum+=c;
        if (c%3==0) n1++;
      }
      if (n1==n2) {
        if (n1%2==0) 
          cout << "T" << endl;
        else
          cout << "S" << endl;
      }
      else if (sum%3==0) {
        if (n1%2==0) 
          cout << "T" << endl;
        else
          cout << "S" << endl;
      }
      else if (n2-n1<=2) {
        if (n1%2==0) 
          cout << "T" << endl;
        else
          cout << "S" << endl;
      }
      else if ((n2-n1)%3==2) {
        if (n1%2==0) 
          cout << "T" << endl;
        else
          cout << "S" << endl;
      }
      else {
        if (n1%2==0) 
          cout << "S" << endl;
        else
          cout << "T" << endl;
      }
    }
  }
  return 0;
}

Re: 11489 - Integer Game how to solve?

Posted: Sat Sep 20, 2008 2:25 pm
by LayCurse
peterkwan wrote:I am getting WA all the time. Please help to give some critical inputs. Thanks.
Try this.
input:
6
443
4444
44444
44455
44555
45555
output:
Case 1: T
Case 2: S
Case 3: T
Case 4: S
Case 5: S
Case 6: T

Re: 11489 - Integer Game how to solve?

Posted: Sun Sep 21, 2008 5:32 am
by peterkwan
LayCurse, thanks for your input. I modified my code and finally passed the test case above. However, I am still getting WA. Could somebody helps what test case my code below will fail? Thanks a lot.

Code: Select all

#include <iostream>
using namespace std;

main() {
  int n;
  cin >> n;
  for (int i=0; i<n; i++) {
    string s;
    cin >> s;
    int n2 = s.size();
    cout << "Case " << (i+1) << ": ";
 //   cout << s << " - ";

    if (n2==1)
      cout << "S" << endl;
    else if (n2==2)
      cout << "T" << endl;
    else {
      int sum = 0, n1 = 0;
      string s1="", s2="";
      for (int j=0;j<n2;j++) {
        int c=(s[j]-'0');
        if (c%3==0) n1++;
        else { 
          if (c%3==1)
            s1+=s[j];
          else
            s2+=s[j];
          sum+=c;
        }      
      }
      if (n1==n2) {
        if (n1%2==0) 
          cout << "T" << endl;
        else
          cout << "S" << endl;
      }
      else if (sum%3==0) {
        if (n1%2==0) 
          cout << "T" << endl;
        else
          cout << "S" << endl;
      }
      else if ((n2-n1)==1) {
        if (n1%2==0) 
          cout << "T" << endl;
        else
          cout << "S" << endl;
      }
      else if ((n2-n1)==2) {
        cout << "T" << endl;
      }
      else {
        if (sum%3==2 && s2.size()>=1) {
          if (n1%2==0)
            cout << "S" << endl;
          else
            cout << "T" << endl;
        }
        else if (sum%3==1 && s1.size()>=1) {
          if (n1%2==0)
            cout << "S" << endl;
          else
            cout << "T" << endl;
        }
        else {
          if (n1%2==0)
            cout << "T" << endl;
          else
            cout << "S" << endl;
        }
      }
    }
  }
  return 0;
}

Re: 11489 - Integer Game

Posted: Sun Jan 25, 2009 12:11 pm
by Obaida
Some one can explain these case???
INPUT:

Code: Select all

2
44455
44555
OUTPUT:

Code: Select all

S
S
Why???
The problem statement says if the number of digits in N is odd then S wins otherwise T wins.
I am confused. :-?

Re: 11489 - Integer Game

Posted: Sun Jan 25, 2009 2:30 pm
by peterkwan
Obaida wrote:Some one can explain these case???
INPUT:

Code: Select all

2
44455
44555
OUTPUT:

Code: Select all

S
S
Why???
I am confused. :-?
This is because:
A player can remove a particular digit if the sum of digits of the resulting number is a multiple of 3 or there are no digits left.
Since S moves first, the obvious way is to remove the redundant digit (4 in the first case and 5 in the second case). After removing the digit, there is no way for T to remove a digit to remain the resulting sum to be a multiple of 3. Therfore, S wins.

Re: 11489 - Integer Game

Posted: Sun Jan 25, 2009 2:46 pm
by sohel
Obaida wrote:Some one can explain these case???
INPUT:

Code: Select all

2
44455
44555
OUTPUT:

Code: Select all

S
S
Why???
The problem statement says if the number of digits in N is odd then S wins otherwise T wins.
I am confused. :-?
You should continue reading on from that sentence. :) The next sentence that follows goes like this : "To make the game more interesting, we apply one additional constraint. A player can remove a particular digit if the sum of digits of the resulting number is a multiple of 3 or there are no digits left."

Hope it clears up things!

Re: 11489 - Integer Game

Posted: Wed Jul 01, 2015 9:11 pm
by ssavi
Why It is Getting WA?? I have Checked All The Tests of Samples , uDebug and OJ Borads . All are Ok . But I am getting WA . Here is my Code: -

Code: Select all

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t, caseno=0;
    char a[1005];
    scanf("%d",&t);
    getchar();
    for(int k=0; k<t; k++)
    {
        gets(a);
        int digsum = 0, res;
        for(int i=0; i<strlen(a); i++)
            { digsum = digsum + (a[i]-'0'); }
        int s = 0;
        int t = 0;
        for(int i=0; i<strlen(a); i++)
        {
            if(a[i]!='0')
            {
                res = digsum - (a[i]-'0');
                if(res%3==0)
                {
                    a[i]='0';
                    digsum = res;
                    if(s==t)
                        s++;
                    else
                        t++;
                }
            }
        }
        //cout<<s<<t<<endl;
        if(s>t)
            printf("Case %d: S\n", ++caseno);
        else
            printf("Case %d: T\n", ++caseno);
    }
    return 0;
}
Please Help Me Out ..

Re: 11489 - Integer Game

Posted: Tue Feb 28, 2017 2:16 pm
by Zyaad Jaunnoo
ssavi wrote:Why It is Getting WA?? I have Checked All The Tests of Samples , uDebug and OJ Borads . All are Ok . But I am getting WA .
.
.
Your code is almost correct.. But, you missed a "break" statement when picking a move.

INPUT

Code: Select all

1
2354456456587544678986756467889867
OUTPUT

Code: Select all

Case 1: S