[Algo Beat 009 & MROI-R1] Number Game

· · 题解

一道简单题。

思路

我们可以设 f(x) 来表示 x 的各位数字之和。每次操作把 x 变成 f(x)。

若输入的 x\le 9,则操作后新数等于自身,当前玩家一定输。 否则我们从 n 开始不断执行 x\leftarrow f(x),直到 x 为个位数,统计中间执行了多少步 s:

证明

第 1,3,5,\dots 步由小 S 执行,到达一位数后下一步的玩家一定会输。 所以如果中间步数为奇数,下一步就会到小 M,所以小 S 赢,否则小 S 输。
时间复杂度 O(T \log n),可以通过此题。

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;
}