codeforces 167C Wizards and Numbers(找规律,博弈)
来源:互联网 发布:apache服务器安装 编辑:程序博客网 时间:2024/05/21 06:28
In some country live wizards. They love playing with numbers.
The blackboard has two numbers written on it — a and b. The order of the numbers is not important. Let's consider a ≤ b for the sake of definiteness. The players can cast one of the two spells in turns:
- Replace b with b - ak. Number k can be chosen by the player, considering the limitations that k > 0 and b - ak ≥ 0. Number k is chosen independently each time an active player casts a spell.
- Replace b with b mod a.
If a > b, similar moves are possible.
If at least one of the numbers equals zero, a player can't make a move, because taking a remainder modulo zero is considered somewhat uncivilized, and it is far too boring to subtract a zero. The player who cannot make a move, loses.
To perform well in the magic totalizator, you need to learn to quickly determine which player wins, if both wizards play optimally: the one that moves first or the one that moves second.
The first line contains a single integer t — the number of input data sets (1 ≤ t ≤ 104). Each of the next t lines contains two integers a, b(0 ≤ a, b ≤ 1018). The numbers are separated by a space.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecificator.
For any of the t input sets print "First" (without the quotes) if the player who moves first wins. Print "Second" (without the quotes) if the player who moves second wins. Print the answers to different data sets on different lines in the order in which they are given in the input.
410 2131 100 110 30
FirstSecondSecondFirst
In the first sample, the first player should go to (11,10). Then, after a single move of the second player to (1,10), he will take 10 modulo 1 and win.
In the second sample the first player has two moves to (1,10) and (21,10). After both moves the second player can win.
In the third sample, the first player has no moves.
In the fourth sample, the first player wins in one move, taking 30 modulo 10.
题意:
给你两个数,两个人轮流进行操作,每轮有两个操作,假设当前这一轮b>a,那么可以把b变成b%a或者是b-a^k。改变后的数不能小于0,当谁不能操作谁就输了。
思路:
每轮操作都可以将(a,b)变成(b%a,a),如果(b%a,a)是必败态,那么(a,b)是必胜态。如果(b%a,a)是必胜态,那么我们可以打表找规律,会发现周期为a+1。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <map> #include <algorithm> #include <set> #include <functional> using namespace std;typedef long long LL;typedef unsigned long long ULL;const int INF = 1e9 + 5;const int MAXN = 100000007;const int MOD = 1e9 + 7;const double eps = 1e-8;const double PI = acos(-1.0);LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a%b); }LL getSG(LL x, LL y){if (x == 0 || y == 0)return 0;LL a = min(x, y);LL b = max(x, y);if (getSG(b%a, a)){b /= a;return !((b % (a + 1)) & 1);}return 1;}int main(){LL a, b;int T;scanf("%d", &T);while (T--){scanf("%lld%lld", &a, &b);if (getSG(a, b) == 0)printf("Second\n");elseprintf("First\n");}}
- codeforces 167C Wizards and Numbers(找规律,博弈)
- 博弈——Wizards and Numbers
- CodeForces - 368C Sereja and Algorithm (找规律&模拟)
- CodeForces - 584C Marina and Vasya (模拟)找规律
- Codeforces 785C Anton and Fairy Tale 找规律 二分
- Codeforces 167 B. Wizards and Huge Prize
- Codeforces#167E Wizards and Bets 高斯消元
- codeforces 24C 找规律
- codeforces round#366(div2) A(暴力)B(博弈 找规律)C(模拟 队列)
- HDOJ1525 [找规律博弈]
- hdu1079 找规律/博弈
- Codeforces 520C. DNA Alignment 找规律
- CodeForces 337C 找规律 ,等比数列求和
- CodeForces 631C Report(找规律)
- codeforces-141【B找规律】【C思维】
- codeforces-743【B找规律】C思维】
- codeforces 167B Wizards and Huge Prize DP 概率
- Codeforces 167B Wizards and Huge Prize 概率dp(水
- IDEA杂记
- Java 表单注册常用正则表达式验证工具类,常用正则表达式大集合。 1. 电话号码 2. 邮编 3. QQ 4. E-mail 5. 手机号码 ……
- Oracle的小笔记,模糊处理人名,电话,地址之类字段数据时可以使用的SQL语句
- 前端---div的60秒倒计时
- 朴素贝叶斯-文本分类侮辱性言论
- codeforces 167C Wizards and Numbers(找规律,博弈)
- SpringBoot + thymeleaf 实现简单的登陆验证
- 6.9
- TensorFlow四种损失函数
- No artifacts found that match the file pattern
- 学习
- NASA 的十大编码准则
- JS去除ajax传过来的字符串空格
- maven pom