一道简单题。
思路
我们可以设 来表示 的各位数字之和。每次操作把 变成 。
若输入的 ,则操作后新数等于自身,当前玩家一定输。 否则我们从 开始不断执行 ,直到 为个位数,统计中间执行了多少步 :
- 为奇数,则小 S 获胜;
- 为偶数:小 M 获胜。
证明
第 步由小 S 执行,到达一位数后下一步的玩家一定会输。
所以如果中间步数为奇数,下一步就会到小 M,所以小 S 赢,否则小 S 输。
时间复杂度 ,可以通过此题。
AC Code
#include<bits/stdc++.h>
#define AC 0
using namespace std;
const int N=1e5+10;
const int M=1e4+10;
const int K=1e3+10;
const int MOD=1e9+7;
const int MOD2=998244353;
const int INF=-1e9;
typedef long long ll;
const ll inf=0x3f3f3f3f3f3f3f3f;
typedef pair<int,int> PII;
int cnt2(ll x){
int s=0;
while(x>0){
s+=x%10;
x/=10;
}
return s;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
int s=0;
while(n>=10){
n=cnt2(n);
s++;
}
if(s&1) cout<<"S"<<endl;
else cout<<"M"<<endl;
}
return AC;
}