UVA 10368 - Euclid's Game(数论+博弈)

10368 - Euclid‘s Game

题目链接

题意:Stan和Ollie玩游戏,有两个数字a,b,每次可以选择较小数字的倍数,把另一个数字-去这个数,要保证>= 0,最后谁那步能得出0谁就赢了,问谁会赢。

思路:其实这个相减的过程就是一个辗转相除的过程,考虑每一次辗转相除,如果只有1倍的数可以减,那么必须到下一步,如果有多步,先手的就有机会选择是自己到下一步或者让对方到下一步,这样先手的就必胜了,于是利用辗转相除,求出谁能先掌控局面,就是谁赢了。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int n, m;

void solve(int a, int b, int who) {
	if (!b || a / b > 1 || a == b) {
		printf("%s wins\n", who == 0? "Stan" : "Ollie");
		return;
	}
	solve(b, a % b, !who);
}

int main() {
	while (~scanf("%d%d", &n, &m) && n + m) {
		if (n < m) swap(n, m);
		solve(n, m, 0);
 	}
	return 0;
}

UVA 10368 - Euclid's Game(数论+博弈),布布扣,bubuko.com

UVA 10368 - Euclid's Game(数论+博弈)

时间: 2024-12-20 21:57:06

UVA 10368 - Euclid's Game(数论+博弈)的相关文章

uva 10368 - Euclid&#39;s Game(博弈)

题目链接:uva 10368 - Euclid's Game 题目大意:给出两个数,两个人做一个游戏,每次有stan开始操作,每次操作可以从最大的数中取走若干个小的数,即a-kb,a为比较大的数,b为比较小的数,kb为取走的值,k必须为整数,并且kb≤a.如果不能顺利执行操作,则对手胜利. 解题思路:模拟,直到k的最大值不为1时,当前操作者就掌握了主动权,既可以获胜.特殊情况为a=b的时候,stan胜. #include <cstdio> #include <cstring> #i

uva 10104 Euclid Problem (数论-扩展欧几里德)

 Euclid Problem  The Problem From Euclid it is known that for any positive integers A and B there exist such integers X and Y that AX+BY=D, where D is the greatest common divisor of A and B. The problem is to find for given A and B corresponding X, Y

UVA 1341 - Different Digits(数论)

UVA 1341 - Different Digits 题目链接 题意:给定一个正整数n,求一个kn使得kn上用的数字最少,如果相同,则输出值最小的 思路: 首先利用鸽笼原理证明最多需要2个数字去组成 设一个数字k,组成k,kk,kkk,kkkk... %n之后余数必然在0 - (n - 1)之间,所以必然能选出两个余数相等的数字相减为0,这个数字就是由0和k组成的. 因此只要考虑一个数字和两个数字的情况,去bfs,记忆化余数,因为余数重复必然形成周期了 代码: #include <stdio.

UVA 10693 10693 - Traffic Volume(数论)

题目链接:10693 - Traffic Volume 根据物理知识, 车经过的时间等于,距离/速度,所以可以列出公式t = (l + d)/v,v/2f + d/v,只有当v / 2f = d/v时,时间最小,v = sqrt(2df),之后时间也能算了. #include <stdio.h> #include <string.h> #include <math.h> double l, f; int main() { while (~scanf("%lf%

UVA 618 - Doing Windows(数论)

题目链接:618 - Doing Windows 题意:给定一个大小不能变的屏幕,和四个大小可以变的窗口,变化要保持长宽比,问这四个窗口能不能调整后全部放下正好填满屏幕,不能重叠 思路:情况一共就几种:4个叠一起,3个叠一起+一个,2个和2个,一个和两个叠一起在一个,把这几种情况全判断了就可以了,判断过程利用gcd,lcm可以求边长. 代码: #include <stdio.h> #include <string.h> long long gcd(long long a, long

UVA 1426 - Discrete Square Roots(数论)

UVA 1426 - Discrete Square Roots 题目链接 题意:给定X, N, R,要求r2≡x (mod n) (1 <= r < n)的所有解,R为一个已知解 思路: r2≡x (mod n)=>r2+k1n=x 已知一个r!,带入两式相减得 r2?r12=kn => (r+r1)(r?r1)=kn 枚举A,B,使得 A * B = n (r + r1)为A倍数 (r - r1)为B倍数 这样就可以推出 Aka?r1=Bkb+r1=r => Aka=Bk

uva 10515 - Powers Et Al.(数论)

题目链接:uva 10515 - Powers Et Al. 题目大意:给出m和n,问说mn的个数上的数是多少. 解题思路:其实只要看m的最后一位数就可以了,判断最有一位的周期,然后用n%t即可. #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 15; const int maxs = 105; vector<int> g

UVA 11490 - Just Another Problem(数论)

11490 - Just Another Problem 题目链接 题意:有S个士兵,排成一个矩阵,矩阵中可以有两个洞,要求两个洞上下左右厚度一样,问能缺少士兵的情况数. 思路:推推公式,设厚度为a, 正方形为i, 那么(3 a + 2 i) (2 a + i) = S + 2 i i; 化简一下得到6 i i + 7 a i = S 由于S很大,所以去枚举厚度,这样只要枚举到sqrt(S)就够了,复杂度可以接受 代码: #include <stdio.h> #include <stri

UVA 417 - Word Index(数论)

题意:417 - Word Index 题意:每个字符串按题目中那样去映射成一个数字,输入字符串,输出数字 思路:这题还是比较水的,由于一共只有83000多个数字,所以对应一个个数字去映射就可以了,注意字符串进位的情况处理即可 代码: #include <stdio.h> #include <string.h> #include <map> #include <string> using namespace std; char str[10]; map<