跳到正文

题解

[Algo Beat 009 & MROI-R1] Number Game

cjz1234约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月18日
收录
2026年7月19日
更新
2026年11月9日
篇幅
约 2 分钟
获赞
3
题目
P17113

版本

  1. v1当前

一道简单题。

思路


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

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

  • ss 为奇数,则小 S 获胜;
  • ss 为偶数:小 M 获胜。

证明


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

AC Code


C++
#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;
}
原文
存档评论2 条抓取于