【HDOJ】4043 FXTZ II
2016-03-02 22:26
288 查看
1. 题目描述
有n个球,第i个球的伤害值为$2^i-1, i \in [1,n]$。有甲乙两个人,每次由甲选择n个球中的一个,用它以相同概率攻击自己或者乙,同时彻底消耗这个球。这样的攻击最多进行n次。
一旦甲的伤害值高于乙,则甲输,否则甲胜。问甲胜的概率是多少。
2. 基本思路
还是一步步推导。令dp[k]表示共有k个球时甲胜的概率。
\begin{align}
dp[1] &= \frac{1}{2} \notag \\
dp[2] &= \frac{1}{2} \times \frac{1}{2} \times (1 + dp[1]) \notag \\
dp[3] &= \frac{1}{3} \times \frac{1}{2} \times (1 + dp[1] + dp[2]) \notag \\
dp[4] &= \frac{1}{4} \times \frac{1}{2} \times (1 + dp[1] + dp[2] + dp[3]) \notag \\
&\cdots \notag \\
dp
&= \frac{1}{n} \times \frac{1}{2} \times (1 + \Sigma_{i=1}^{n-1}dp[i])
\end{align}
为什么上式成立,以$dp[3] = \frac{1}{3} \times \frac{1}{2} \times (1 + dp[1] + dp[2])$为例解释。
$\frac{1}{3} \times \frac{1}{2}$表示在第k次取到第3个球的概率(该球一定攻击乙),$k \in [1,3]$。
此时,这个球一定属于乙(否则甲必输)并且从此时开始,无论后续的球如何安排,最终都是甲胜。
然而,前k次一定满足甲胜,否则在$[1,k-1]$的某一次中,即停止游戏。
当k=3时,概率为dp[2];
当k=2时,概率为dp[1];
当k=1时,概率为1。
以此类推,dp
。
3. 代码
有n个球,第i个球的伤害值为$2^i-1, i \in [1,n]$。有甲乙两个人,每次由甲选择n个球中的一个,用它以相同概率攻击自己或者乙,同时彻底消耗这个球。这样的攻击最多进行n次。
一旦甲的伤害值高于乙,则甲输,否则甲胜。问甲胜的概率是多少。
2. 基本思路
还是一步步推导。令dp[k]表示共有k个球时甲胜的概率。
\begin{align}
dp[1] &= \frac{1}{2} \notag \\
dp[2] &= \frac{1}{2} \times \frac{1}{2} \times (1 + dp[1]) \notag \\
dp[3] &= \frac{1}{3} \times \frac{1}{2} \times (1 + dp[1] + dp[2]) \notag \\
dp[4] &= \frac{1}{4} \times \frac{1}{2} \times (1 + dp[1] + dp[2] + dp[3]) \notag \\
&\cdots \notag \\
dp
&= \frac{1}{n} \times \frac{1}{2} \times (1 + \Sigma_{i=1}^{n-1}dp[i])
\end{align}
为什么上式成立,以$dp[3] = \frac{1}{3} \times \frac{1}{2} \times (1 + dp[1] + dp[2])$为例解释。
$\frac{1}{3} \times \frac{1}{2}$表示在第k次取到第3个球的概率(该球一定攻击乙),$k \in [1,3]$。
此时,这个球一定属于乙(否则甲必输)并且从此时开始,无论后续的球如何安排,最终都是甲胜。
然而,前k次一定满足甲胜,否则在$[1,k-1]$的某一次中,即停止游戏。
当k=3时,概率为dp[2];
当k=2时,概率为dp[1];
当k=1时,概率为1。
以此类推,dp
。
3. 代码
import java.lang.*; import java.io.*; import java.util.*; import java.math.BigInteger; public class Main { public static void main(String[] arg) throws java.lang.Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(in, out); out.close(); } } class TaskA { public final static int maxn = 505; BigInteger[] FZ = new BigInteger[maxn]; BigInteger[] FM = new BigInteger[maxn]; public TaskA() { init(); } public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); int n; while (t-- > 0) { n = in.nextInt(); out.println(FZ .toString() + "/" + FM .toString()); } } private void init() { BigInteger sfm = BigInteger.ONE, sfz = BigInteger.ONE; BigInteger fm, fz; BigInteger g, lcm; for (int i=1; i<=500; ++i) { fm = sfm.multiply(BigInteger.valueOf(i*2)); fz = sfz; g = fz.gcd(fm); FZ[i] = fz.divide(g); FM[i] = fm.divide(g); // System.out.println(fz + "/" + fm); g = sfm.gcd(FM[i]); sfz = sfz.multiply(FM[i].divide(g)) .add( FZ[i].multiply(sfm.divide(g)) ); sfm = FM[i].divide(g).multiply(sfm); } } private BigInteger A(int n, int m) { BigInteger ret = BigInteger.ONE; for (int i=n; i>n-m; --i) ret = ret.multiply(BigInteger.valueOf(i)); return ret; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer==null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
相关文章推荐
- NetBeans下Tomcat无法查看web应用运行,报错:部署错误
- Android软件开发随笔笔记第一讲
- 【杭电】[2048]神、上帝以及老天爷
- BASIC-15 字符串对比 字符串 大小写 VIP试题
- Python 第五天 装饰器
- Virtual Box上安装CentOS7
- bzoj 1079: [SCOI2008]着色方案
- POJ 3176 Cow Bowling
- 三条足以让电脑死机的命令(.bat文件)
- ListView在Activity和Fragment中的显示和使用
- Hibernate的延迟加载 ,懒加载,lazy
- passwd修改用户密码
- 线程、主线程、进程、操作系统、计算机硬件一些概念笔记和疑问
- sessionStorage localStorage 和 cookie 之间的区别转
- Xcode7 下导入第三方库 图文介绍
- try块和异常处理,断言assert
- Android Camera数据流分析全程记录(非overlay方式)
- Educational Codeforces Round 9 B. Alice, Bob, Two Teams 前缀和
- [转] 机器视觉开源代码集合
- 学习c语言