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 | Case 1: S |
Problem Setter: Sohel Hafiz
Special Thanks: Shamim Hafiz, Md. Arifuzzaman Arif
解题报告:在n中取数字,使剩下的数是3的倍数(包括0),不能取则失败。
分析一下,如果能使当前数为3的倍数,数字和必是3的倍数。要保持这种状态,必须每次都取模3为0的数字,直到取完,则对手失败。如果不能使其转化为3的倍数的状态,则当前玩家失败。所以,代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[2000];
void work()
{
scanf("%s",str);
int len=strlen(str);
int d[3]={0,0,0};
for(int i=0;i<len;i++)
d[(str[i]-'0')%3]++;
int res = (d[1]+d[2]*2)%3;
bool flag=true;
if(res==0) flag=d[0]%2;
else if(d[res]>0) flag=(d[0]%2==0);
else flag=false;
puts(flag?"S":"T");
}
int main()
{
int T;
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
printf("Case %d: ", i);
work();
}
}