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 | Case 1: S |
Problem Setter: Sohel Hafiz
Special Thanks: Shamim Hafiz, Md. Arifuzzaman Arif
感觉题目意思有歧义。。。看了样例才明白的!
题目意思就是S先手取数 是剩下的数满足能被3整除。。。接着两人轮流取 到不能取为止
不能取的人输
因为 3 ,6 ,9这些数对3求余是等价的
1,4,7也一样
所以取1或4不会对最后结果造成影响!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
string st;
bool vis[1010];
int sum,cnt;
void init(){
memset(vis,0,sizeof vis);
sum = 0;
cnt = st.size();
}
int main(){
int ncase,T=1;
cin >> ncase;
while(ncase--){
cin >> st;
init();
for(int i = 0; i < st.size(); i++){
sum = (sum+st[i]-'0')%3;
}
//cout<<sum<<endl;
int turn = 0;
if(sum!=0){
for(int i = 0; i < st.size(); i++){
if((st[i]-'0')%3==sum){
vis[i] = 1;
//cout<<st[i]<<endl;
cnt--;
turn = 1;
break;
}
}
}
while(1){
bool flag = 1;
for(int i = 0; i < st.size(); i++){
if(!vis[i]&&(st[i]-'0')%3==0){
cnt--;
vis[i] = 1;
flag = 0;
if(turn) turn = 0;
else turn = 1;
}
}
if(flag) break;
}
printf("Case %d: ",T++);
if(turn==1) cout<<"S"<<endl;
else cout<<"T"<<endl;
}
return 0;
}