UVA 11489 Integer Game (博弈)

1227: Integer Game

Time Limit: 1 Sec  Memory Limit:128 MB

Submit: 9  Solved: 4

[Submit][Status][Web
Board
]

Description

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, its 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

3
4
33
771

Sample Output

Case 1: S
Case 2: T
Case 3: T

解析:

1.总和不为3的倍数时,判断第一个人能否取。

(1)能取,判断有多少个数为三的倍数即可。

(2)不能取, 输出T。

2.总和为3的倍数时,直接判断判断有多少个数为三的倍数即可。

AC代码;

#include <bits/stdc++.h>
using namespace std;

char s[1002];
bool vis[4];

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    #endif // sxk

    int T;
    scanf("%d", &T);
    for(int t=1; t<=T; t++){
        memset(vis, false, sizeof(vis));
        printf("Case %d: ", t);
        scanf("%s", s);
        int len = strlen(s);
        int cnt = 0, sum = 0;
        for(int i=0; i<len; i++){
            int foo = s[i] - '0';
            sum += foo;
            if(foo % 3 == 0) cnt ++;
            vis[foo % 3] = true;
        }
        if(sum % 3){
            if(vis[sum % 3]) puts(cnt % 2 ? "T" : "S");
            else puts("T");
        }
        else puts(cnt % 2 ? "S" : "T");
    }
    return 0;
}
时间: 2024-10-19 08:12:45

UVA 11489 Integer Game (博弈)的相关文章

Uva 11489 - 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 ga

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

UVA 1558 - Number Game(博弈dp)

UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次可以选一个数字,然后它的倍数,还有其他已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp记忆化去求解,要输出方案就枚举第一步即可,状态转移过程中,选中一个数字,相应的变化写成一个函数,然后就是普通的博弈问题了,必胜态之后必有必败态,必败态之后全是必胜态 代码: #include <stdio.h> #include <string.h> const int N = 105

UVA 1557 - Calendar Game(博弈dp)

UVA 1557 - Calendar Game 题目链接 题意:给定一个日期,两个人轮流走,每次可以走一月或者一天,问最后谁能走到2001.11.4这个日子 思路:记忆化搜索,对于每个日期,如果下两个状态有一个非必胜态,那么这个状态是必胜态,如果后继状态都是必胜态,那么该状态为必败态 代码: #include <stdio.h> #include <string.h> const int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31,

uva 1557 - Calendar Game(博弈)

题目链接:uva 1557 - Calendar Game 题目大意:给定一个日期,每次可以选择加一个月,或者加一天,加一个月的前提是下一个月有对应的日期,比如1.30加一个月变成2.30是不合法的,日期上限为2001.11.4.两个人轮流操作,不能操作为失败. 解题思路:dp[y][m][d]表示对应日期是否为先手必胜.预先处理即可,注意细节,包括闰年等.分享代码. #include <cstdio> #include <cstring> #include <algorit

UVA 12293 - Box Game(博弈)

UVA 12293 - Box Game 题目链接 题意:两个盒子,一开始一个盒子有n个球,一个只有1个球,每次把球少的盒子中球消掉,把多的拿一些球给这个盒子,最后不能操作的输(球不能少于1个),Alice先手,问谁赢 思路:博弈,题目其实可以转化为,给定一个n,每次把减少1到n/2的数字,最后谁是1谁就输了,那么可以去递推前几项找个规律,或者推理,都可以发现只要是2^i - 1的数字Bob就赢,否则Alice赢 代码: #include <stdio.h> #include <stri

UVa 424 Integer Inquiry 【大数相加】

解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入a的第一个字符为0的时候结束运算,输出结果.  Integer Inquiry  One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3

UVA 11892 ENimEN (简单博弈)

题面: 11892  ENimEN In deterministic games no chance is involved, meaning that the final result can be predicted from the initial arrangement assuming players play optimal. These games are so boring.piloop and poopi are professional gamers. They play g

UVA 10891 区间DP+博弈思想

很明显带有博弈的味道.让A-B最大,由于双方都采用最佳策略,在博弈中有一个要求时,让一方的值尽量大.而且由于是序列,所以很容易想到状态dp[i][j],表示序列从i到j.结合博弈中的思想,表示初始状态i->j情况下,先手能获得的最大分数.后手能获得的就是sum[i][j]-dp[i][j].接下来枚举先手选取的是两端的哪一段即可. #include <iostream> #include <cstdio> #include <cstring> using name