SWUST OJ#99 欧几里得博弈

本文探讨了一种两人博弈游戏,从两个不等的正数开始,玩家轮流写下棋盘上已有数字的差值。通过分析游戏规则,我们利用数论中的最大公约数(GCD)和费马-欧拉定理来确定先手或后手的优势。当最大公约数为奇数时,先手(A)获胜;反之,若为偶数,则后手(B)获胜。这种策略在数学和博弈论中具有重要意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

题目

思路

代码


题目

题目描述

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"<<endl;
else cout<<"B"<<endl;

代码

由于题目已经明确a>b,所以不用在max了。且判断奇偶也与b无关。则最终代码可舍去该步骤。

#include <bits/stdc++.h>
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"<<endl;
    else cout<<"B"<<endl;
    return 0;
}

### SWUST OJ Problem 32 Information and Solution Unfortunately, specific details about SWUST OJ problem number 32 are not directly provided in the available references. However, based on similar problems from this platform such as those mentioned in other citations, a general approach to solving typical programming challenges can be outlined. #### Understanding Common Elements of Programming Problems on SWUST OJ Platform Problems like SWUSTOJ276, SWUSTOJ77, SWUSTOJ78, SWUSTOJ1286, and SWUSTOJ1285 emphasize proper use of `if` and `else` statements along with maintaining good coding practices including appropriate formatting[^1]. For instance, when dealing with numerical outputs, `%g` is used for automatic selection between fixed-point notation (`%f`) or scientific notation (`%e`), depending on which provides more compact output without loss of precision. Given that detailed specifics regarding problem 32 aren't present here, one should look at common patterns found across different types of questions posed by platforms like these: - **Input Handling**: Typically involves reading inputs either single values or arrays/lists. - **Logic Implementation**: Applying algorithms ranging from simple arithmetic operations up through complex data structures manipulation. - **Output Formatting**: Ensuring results adhere strictly to specified formats using placeholders like `%d`, `%s`, etc., where applicable. Since no direct reference exists specifically addressing SWUST OJ problem 32 within given sources, consider exploring adjacent numbered problems around it for clues about its nature—whether mathematical computation, string processing, dynamic programming elements, et cetera—and adapt solutions accordingly while keeping best practice guidelines intact. ```c // Example C code snippet demonstrating basic structure often seen in contest-style programs #include <stdio.h> int main() { int n; scanf("%d", &n); // Read input value if (condition_based_on_problem_statement) { printf("Result under condition A\n"); } else { printf("Alternative result\n"); } return 0; } ``` --related questions-- 1. How does understanding how `%g` works help improve program efficiency? 2. What strategies could apply towards optimizing performance in competitive programming contests? 3. Can you provide examples illustrating effective usage of conditional operators (`if`, `else`) in algorithm design?
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青衿白首志

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值