UASCO Bronze 2024 February Problem1 Palindrome Game

Problem

Bessie and Elsie are playing a game with a pile of stones that initially contains S stones (1 \leq S < 10 ^ {10 ^ 5}). The two cows alternate turns, with Bessie going first. When it is a cow's turn, she must remove x stones from the pile, where x is any positive integer palindrome of the cow's choosing. If the pile is empty when a cow's turn starts, that cow loses.

Definition: A positive integer is a palindrome if it reads the same forward and backward; examples of palindromes include 1, 121, and 9009. Leading zeros are not allowed; e.g., 990 is *not* a palindrome.

There are T (1 \leq T \leq 10) independent test cases. For each test case, print who wins the game if both cows play optimally.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains T, the number of test cases. The next T lines describe the test cases, one line per test case.

Each test case is specified by a single integer S.

OUTPUT FORMAT (print output to the terminal / stdout):

For each test case, output B if Bessie wins the game under optimal play starting with a pile of stones of size S, or E otherwise, on a new line.

SAMPLE INPUT:

3
8
10
12

SAMPLE OUTPUT:

B
E
B

For the first test case, Bessie can remove all the stones on her first move, since 88 is a palindrome, guaranteeing her win.

For the second test case, 10 is not a palindrome, so Bessie cannot remove all the stones on her first move. Regardless of how many stones Bessie removes on her first move, Elsie can always remove all remaining stones on her second move, guaranteeing her win.

For the third test case, it can be proven that Bessie wins under optimal play.

SCORING:

  • Inputs 2-4: S < 100
  • Inputs 5-7: S < 10 ^ 6
  • Inputs 8-10: S < 10^9
  • Inputs 11-13: No additional constraints.

 Solution

算法与解法

(讲得有可能不清楚,请原谅)

一看到  S 远远超出 64 位整数类型所能存储,又因为它出现在 Bronze 第一题,可以排除高精度算法了。既然不用高精度,那只能找规律了。

 找规律,首先就要枚举一下 S  小数值,看一下获胜情况:

SBE
11(win)/
22(win)/
33(win)/
44(win)/
……(n)……[n(win)]……(/)
1055(win)
1111(win)/
122+1(win)9
133+1(win)9
144+1(win)9
155+1(win)9
166+1(win)9
177+1(win)9
188+1(win)9
199+1(win)9
201+19+9(win)
2111+9(win)1
2222(win)/
………………

n_1 + n_2 + ··· + n_k,其中  n_i 表示奶牛在第 i 轮中拾取的石头,皆按最佳选择)

从上表中,看出 1 \leq S \leq 9 时,B 作为先手,只要在第一轮拿走全部的石头,即可获胜。

10 \leq S时,通过枚举可以发现一个规律(“无敌状态”):当一头奶牛将一些石子拿走,剩余的石子个数为 10 的倍数,另一头奶牛必输无疑。

因为当一头奶牛拿完石子剩余的石子个数为 10 的倍数时,另一只奶牛拿任何回文数数量的石子,都不可能使剩余的石子个数仍为 10 的倍数,除非拿去一个 10 的倍数的回文数,但是题目告诉我们:回文数不可前导 0 ,所以最末位也不可为 0。前一只奶牛可以重新拿完石子使石子剩余的石子个数为 10 的倍数(拿当前石子剩余个数除以 10 的余数即可),这样一直重复,如果另一只奶牛拿走石子使石子剩余个数比 10 小或是为其他回文数,前一只奶牛可以直接拿完剩下获得胜利。

发现这个规律后,我们发现当 S % 10 != 0 时,B 只要拿走当前石子剩余个数除以 10 的余数,就可以进入“无敌状态”。而当 S % 10 = 0 ,B 拿走任意个都不可能使剩余个数为10的倍数,而 E 只要拿走当前石子剩余个数除以 10 的余数,可以进入“无敌状态”。

 所以在这个题目中,只要 S 的最后一位是 0(S % 10 = 0),E 获胜;其余情况(S % 10 != 0),B 获胜。

代码(相当简单)

#include <bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t --){
		string str;
		cin >> str;
		if(str[str.size() - 1] == '0')
			cout << "E\n";
		else
			cout << "B\n";
	}
	return 0;
}

吐槽一下:搞了几个LaTeX 公式花费了好多时间,每个公式都需要在上方创建

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值