HDU 5953 Game of Taking Stones(威佐夫博弈+高精度+二分)——The 2016 ACM-ICPC Asia Dalian Regional Contest
2017-06-04 15:50
603 查看
传送门
Two people face two piles of stones and make a game. They take turns to take stones. As game rules, there are two different methods of taking stones: One scheme is that you can take any number of stones in any one pile while the alternative is to take the same amount of stones at the same time in two piles. In the end, the first person taking all the stones is winner.Now,giving the initial number of two stones, can you win this game if you are the first to take stones and both sides have taken the best strategy?
[align=left]Input[/align] Input contains multiple sets of test data.Each test data occupies one line,containing two non-negative integers a andb,representing the number of two stones.a and b are not more than 10^100.
[align=left]Output[/align] For each test data,output answer on one line.1 means you are the winner,otherwise output 0.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
题目大意:
两堆石子,分别 a 和 b 个,A 和 B 两个人进行一场游戏,每个人可以从一堆石子中拿任意个,或者从两堆石子中拿相同的个数,谁没得拿谁输。就是一个威佐夫博弈,但是数据范围太大, a,b≤10100
解题思路:
二分精度,把精度精确到一百位左右就OK了,用 java 操作比较方便, 有BigDecimal,直接二分,然后判断以下就OK了。
代码:
Two people face two piles of stones and make a game. They take turns to take stones. As game rules, there are two different methods of taking stones: One scheme is that you can take any number of stones in any one pile while the alternative is to take the same amount of stones at the same time in two piles. In the end, the first person taking all the stones is winner.Now,giving the initial number of two stones, can you win this game if you are the first to take stones and both sides have taken the best strategy?
[align=left]Input[/align] Input contains multiple sets of test data.Each test data occupies one line,containing two non-negative integers a andb,representing the number of two stones.a and b are not more than 10^100.
[align=left]Output[/align] For each test data,output answer on one line.1 means you are the winner,otherwise output 0.
[align=left]Sample Input[/align]
2 1 8 4 4 7
[align=left]Sample Output[/align]
0 1 0
题目大意:
两堆石子,分别 a 和 b 个,A 和 B 两个人进行一场游戏,每个人可以从一堆石子中拿任意个,或者从两堆石子中拿相同的个数,谁没得拿谁输。就是一个威佐夫博弈,但是数据范围太大, a,b≤10100
解题思路:
二分精度,把精度精确到一百位左右就OK了,用 java 操作比较方便, 有BigDecimal,直接二分,然后判断以下就OK了。
代码:
import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String[] args){ BigDecimal a, b, tmp, gold; BigDecimal l, r, eps, mid; l = BigDecimal.valueOf(2.236067); r = BigDecimal.valueOf(2.236068); mid = BigDecimal.valueOf(2.236067); eps = BigDecimal.valueOf(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001); while(r.subtract(l).compareTo(eps) > 0){ mid = l.add(r).divide(BigDecimal.valueOf(2)); if(BigDecimal.valueOf(5).subtract(mid.multiply(mid)).compareTo(eps)<0) r = mid; else l = mid; } gold = mid.add(BigDecimal.ONE).divide(BigDecimal.valueOf(2)); Scanner in = new Scanner(System.in); while(in.hasNextBigDecimal()){ a = in.nextBigDecimal(); b = in.nextBigDecimal(); if(a.compareTo(b)>0){ tmp = a; a = b; b = tmp; } BigDecimal c = b.subtract(a).multiply(gold); if(c.setScale(0,BigDecimal.ROUND_DOWN).equals(a)==true) System.out.println("0"); else System.out.println("1"); } } }
相关文章推荐
- The 2016 ACM-ICPC Asia Dalian Regional Contest(汇总)
- The 2016 ACM-ICPC Asia Dalian Regional Contest
- HDU 5875 Function(二分区间+RMQ)——2016 ACM/ICPC Asia Regional Dalian Online
- HDU 3681 Prison Break 二分+搜索 The 35th ACM-ICPC Asia Regional Contest (Hangzhou)
- HDU 5875 Function 2016 ACM/ICPC Asia Regional Dalian Online
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
- The 2011 ACM-ICPC Asia Dalian Regional Contest-D Hexadecimal View 【模拟】
- The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
- HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
- 2016 ACM/ICPC Asia Regional Dalian Online 1008 Function 二分+RMQ
- The 2011 ACM-ICPC Asia Dalian Regional Contest——D Hexadecimal View
- 2016 ACM/ICPC Asia Regional Dalian Online 1006 football game 1007 hdu 5873 hdu 5874
- 【2015-2016 ACM-ICPC, NEERC, Northern Subregional Contest J】【二分答案+multiset】Journey to the “The World’
- Hdu 4057 Rescue the Rabbit (AC自动机+状态压缩dp) - 2011 ACM-ICPC Dalian Regional Contest Problem G
- 树状数组+离散化——HDU 5877 (2016 ACM/ICPC Asia Regional Dalian Online 1010)
- HDU 5873 2016 ACM/ICPC Asia Regional Dalian Online 1006 Football Games
- 2016 ACM/ICPC Asia Regional Dalian Online HDU 5877 Weak Pair treap + dfs序
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)