天天看点

UVa 11489 - Integer Game (简单博弈 脑筋急转弯)Sample Input                             Output for Sample Input

I Integer Game

Two players, S and T, are playing a game where they make alternate moves. S plays first. 

In this game, they start with an integer N. In each move, a player removes one digit from the integer and passes the resulting number to the other player. The game continues in this fashion until a player finds he/she has no digit to remove when that player is declared as the loser.

With this restriction, it’s obvious that if the number of digits in N is odd then S wins otherwise T wins. 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.

Suppose N = 1234. S has 4 possible moves. That is, he can remove 1, 2, 3, or 4.  Of these, two of them are valid moves.

- Removal of 4 results in 123 and the sum of digits = 1 + 2 + 3 = 6; 6 is a multiple of 3.

- Removal of 1 results in 234 and the sum of digits = 2 + 3 + 4 = 9; 9 is a multiple of 3.

The other two moves are invalid.

If both players play perfectly, who wins?

Input

The first line of input is an integer T(T<60) that determines the number of test cases. Each case is a line that contains a positive integer N.N has at most 1000 digits and does not contain any zeros.

Output

For each case, output the case number starting from 1. If S wins then output ‘S’ otherwise output ‘T’.

Sample Input                             Output for Sample Input

3

4

33

771

Case 1: S

Case 2: T

Case 3: T

Problem Setter: Sohel Hafiz

Special Thanks: Shamim Hafiz, Md. Arifuzzaman Arif

题意:

给出一个数字串N,两个人轮流从中取出一个数字,要求每次取完之后剩下的数之和是3的倍数(这里之前看lrj的描述理解错了)。不能取者输。问先手的输赢

如果可以,第一次取走后变为了3的倍数,则以后取的一定是3的整数倍,直至取完所有是3的整数倍的数。

所以先统计MOD 3为0 1 2的数有多少,然后先手先取一个数使得其他数之和MOD 3 = 0,不能取则输出T

然后轮到第二个人取数了,统计MOD 3==0的数有多少,如果是偶数,则先手胜,反之先手输。

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 1000 + 20;
char str[maxn];
int cnt[3];

int main() {
    int T;

    scanf("%d", &T);
    for(int kase=1; kase<=T; kase++) {
        scanf("%s", str);
        int n = strlen(str);
        int sum = 0;
        cnt[0] = cnt[1] = cnt[2] = 0;
        for(int i=0; i<n; i++) {
            sum += str[i] - '0';
            cnt[(str[i]-'0')%3]++;
        }
        int t = sum % 3;
        cnt[t]--;
        printf("Case %d: ", kase);
        if(cnt[t] < 0) {
            puts("T");
            continue;
        }
        t = cnt[0];
        if(t&1) puts("T");
        else puts("S");
    }

    return 0;
}