目录
题目描述
Starts with two unequal positive numbers (M,N and M>N) on the board. Two players move in turn. On each move, a player has to write on the board a positive number equal to the difference of two numbers already on the board; this number must be new, i.e., different from all the numbers already on the board. The player who cannot move loses the game. Should you choose to move first or second in this game?
According to the above rules, there are two players play tihs game. Assumptions A write a number on the board at first, then B write it.
Your task is write a program to judge the winner is A or B.翻译:
题目描述
从板上两个不相等的正数(M,N 和 M>N)开始。两个玩家依次移动。在每次移动时,玩家必须在棋盘上写一个正数,该正数等于棋盘上已有的两个数字的差值;此数字必须是新的,即与板上已有的所有数字不同。不能移动的玩家将失去游戏。在这个游戏中,你应该选择先移动还是第二移动?
根据上述规则,有两名玩家玩这个游戏。假设A首先在板上写一个数字,然后B写它。
你的任务是写一个程序来判断赢家是A还是B。
输入
Two unequal positive numbers M and N , M>N (M<1000000)
输出
A or B
翻译:输入两个不等的正数 M 和 N
样例输入
3 1
样例输出
A
本题是经典的博弈论。
跟据题目所说:每次都可以从场上选两个数,写出它们的差值。
假设场上是a,b且 a比b大的多
1:第一个只能写a-b
2:第二个也只能写 (a-b)-b=a-2b
3:第三个就可以写:a-2b-b=a-3b 或者 a-b-(a-2b)=3b 或者 a-(a-2b)=2b。
.......
一直这样推算下去,我们可以发现,最终能写下的就是 a的n倍减去b的m倍,即:a*n-b*m (n,m为整数,可以为负),但不能超过max(a,b),也不能小于0
根据费马-欧拉定理(数论同余),我们可以得知 a*n-b*m 的值总为 gcd(a,b) (a,b的最大公约数)的倍数,结合题目条件,假设可以写的数为c,即: c=k*gcd(a,b)(k为正整数)且 max(a,b)> c >0 。
举个例子,假设a=14,b=8,gcd(14,8)=2
那么场上可以有的数就是2 4 6 8 10 12 14。
再举个例子a=7,b=4 gcd(7,4)=1
那么场上可以有的数就是1 2 3 4 5 6 7
仔细观察上述两组例子,第一组是第二组的两倍,那么最后能写下的结果也是两倍。因此,我们认为这两组的等价的。
所以在最开始我们可以同时除最大公约数(以下a,b默认已最小化),这样我们可以写下的就是从1~max(a,b) 之间且连续的数。
- int mod=__gcd(a,b);
- a/=mod,b/=mod;
由于是A先写
如果说max(a,b)为偶数,那么最后可以写的数也是偶数个,那么B写完后,A写不了,B必赢
如果说max(a,b)为奇数,那么最后可以写的数也是奇数个,那么A写完后,B写不了,A必赢
所以进行奇偶判断输出结果。
- a=max(a,b);
- if(a&1) cout<<"A"<
- else cout<<"B"<
代码
由于题目已经明确a>b,所以不用在max了。且判断奇偶也与b无关。则最终代码可舍去该步骤。
- #include
- typedef long long ll;
- #define N 10005
- #define endl '\n'
- using namespace std;
- int main() {
- int a,b;cin>>a>>b;
- int mod=__gcd(a,b);
- a/=mod;
- if(a&1) cout<<"A"<
- else cout<<"B"<
- return 0;
- }
-
相关阅读:
GitHub
【Spring-2】refresh方法中的invokeBeanFactoryPostProcessors方法
SpringBoot:(四)底层注解详解
如何解决Smartsheet 登录时遇到的问题
DataX 自学使用
【已解决】 Expected linebreaks to be ‘LF‘ but found ‘CRLF‘.
开源共建 | Dinky 扩展批流统一数据集成框架 ChunJun 的实践分享
LinkSLA坚持用户第一,打造可持续的运维服务方案
关于笔试编程题被坑的输入问题,acm模式下的python输入究竟如何写?
商城免费搭建之java商城 开源java电子商务Spring Cloud+Spring Boot+mybatis+MQ+VR全景+b2b2c
-
原文地址:https://blog.csdn.net/qq_62102968/article/details/126695410
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU