[51Nod 1185 威佐夫游戏 V2]Wythoff Game+乘法模拟
来源:互联网 发布:新业软件试用版 编辑:程序博客网 时间:2024/05/16 11:20
[51Nod 1185 威佐夫游戏 V2]Wythoff Game+乘法模拟
题目链接:[51Nod 1185 威佐夫游戏 V2]
题意描述:有2堆石子。A B两个人轮流拿,A先拿。每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取。拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出2堆石子的数量,问最后谁能赢得比赛。
例如:2堆石子分别为3颗和5颗。那么不论A怎样拿,B都有对应的方法拿到最后1颗。
注意:这个题目每堆石子的数目是在
解题思路:根据Wythoff 博弈定理:当且仅当
这题还有一个Java大数的做法, 请参考另外一篇博客《 [hdu 5973 Game of Taking Stones] Wythoff Game+大数运算 》
#include <map>#include <set>#include <queue>#include <cmath>#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>using namespace std;//#pragma comment(linker, "/STACK:1024000000,1024000000")#define FIN freopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w",stdout)#define fst first#define snd second#define rep(i, f, t) for(int i = (f); i <= (t); i++)//typedef __int64 LL;typedef long long LL;typedef unsigned long long ULL;typedef pair<int, int> PII;const ULL MOD = 1000000000; /// 1e9int T;ULL A, B;ULL X[] = {618033988, 749894848, 204586834};ULL Y[2], Z[4];bool check(ULL x, ULL y) { if(x > y) swap(x, y); /// 转化为1e9进制数的模拟乘法,计算k*618033988749894848204586834,结果保存在Z[]中 ULL k = y - x, temp = 0; Y[0] = k / MOD, Y[1] = k % MOD; Z[3] = (temp = X[2] * Y[1] + temp) % MOD, temp /= MOD; Z[2] = (temp = X[1] * Y[1] + X[2] * Y[0] + temp) % MOD, temp /= MOD; Z[1] = (temp = X[0] * Y[1] + X[1] * Y[0] + temp) % MOD, temp /= MOD; Z[0] = (temp = X[0] * Y[0] + temp) % MOD, temp /= MOD; return x == temp * MOD + k + Z[0];}int main() {#ifndef ONLINE_JUDGE FIN;#endif // ONLINE_JUDGE scanf("%d", &T); while(T --) { cin >> A >> B; puts(check(A, B) ? "B" : "A"); } return 0;}
补充一个Java代码:
import java.math.BigDecimal;import java.util.Scanner;public class Main { static BigDecimal sqrt_five, G; static BigDecimal two, five; public static void init() { two = new BigDecimal(2); five = new BigDecimal(5); BigDecimal low, high, mid; low = new BigDecimal(0); high = new BigDecimal(5); mid = new BigDecimal(0); for(int i = 0; i < 100; i++) { mid = low.add(high).divide(two); if(mid.multiply(mid).compareTo(five) > 0) high = mid; else low = mid; } sqrt_five = mid; // G = (sqrt(5) + 1) / 2; G = sqrt_five.add(BigDecimal.ONE).divide(two); } public static void main(String[] argv) { BigDecimal a, b, c, left, right; init(); int T; Scanner input = new Scanner(System.in); T = input.nextInt(); while ((T --) != 0) { a = input.nextBigDecimal(); b = input.nextBigDecimal(); if (a.compareTo(b) > 0) { c = a; a = b; b = c; } left = b.subtract(a).multiply(G); right = a; // 向下取整。统一精度。 left = left.setScale(0, BigDecimal.ROUND_DOWN); right = right.setScale(0, BigDecimal.ROUND_DOWN); if(left.compareTo(right) == 0) System.out.println("B"); else System.out.println("A"); } return; }}
1 0
- [51Nod 1185 威佐夫游戏 V2]Wythoff Game+乘法模拟
- 51nod--1185 威佐夫游戏 V2 (博弈, 乘法模拟)
- 51nod 1185 威佐夫游戏 V2 (博弈+大数乘法模拟)
- 51nod 1185 威佐夫游戏 V2 (用乘法模拟解决大数精度问题)
- 模拟乘法-51nod1185 威佐夫游戏 V2
- 51NOD 1072 Wythoff Game
- 1185 威佐夫游戏v2,卡精度,乘法模拟
- HDU 5973Game of Taking Stones 51nod 1185 威佐夫游戏 V2(威佐夫博弈)
- 51nod 1185 威佐夫游戏V2
- 51nod 1185 威佐夫游戏 V2
- 51nod 1185 威佐夫游戏 V2
- 51 NOD _ 1185 威佐夫游戏 V2
- 51Nod-1185-威佐夫游戏 V2
- 51nod 1185 威佐夫游戏 V2
- 51nod 1185 威佐夫游戏 V2(威佐夫博弈)
- 51nod 1185 威佐夫游戏 V2(威佐夫博弈)
- 51Nod-1185-威佐夫游戏 V2
- 51NOD 1185 威佐夫游戏 V2(博弈论 + 减少精度)
- Spark常用函数讲解之Action操作
- html5 新的选择器
- HTML——jshtml学习笔记+实例+正则表达式+简单轮播+表格增删改查
- springmvc+mybatis显示正确的日期时间的方法
- 【Codeforces Round #376 (Div. 2)】 Codeforces 731A Night at the Museum
- [51Nod 1185 威佐夫游戏 V2]Wythoff Game+乘法模拟
- Spark的 DAGschedule & task schedule 区别以及相互联系
- dagger2 让你爱不释手:重点概念讲解、融合篇
- 如何成功激活win10专业版
- APIStudio中如何导入已有项目?
- 51nod 1284 2 3 5 7的倍数(容斥定理)
- PHP面向对象_自动加载类__autoload()
- caffe windows编译及配置
- GET方法和POST获取中文参数出现乱码的问题及解决方法