POJ P2096 Collecting Bugs
思路
分类讨论,不妨先设DP[i][j]DP[i][j]表示已经发现ii种子系统中有nn种bugbug无非只有四种情况
- 发现的bugbug在旧的系统旧的分类,概率p1p1是(i/s)∗(j/n)(i/s)∗(j/n).
- 发现的bugbug在旧的系统新的分类,概率p2p2是(i/s)∗((n−j)/n)(i/s)∗((n−j)/n)
- 发现的bugbug在新的系统旧的分类,概率p3p3是((s−i)/s)∗(j/n)((s−i)/s)∗(j/n)
- 发现的bugbug在新的系统新的分类,概率p4p4是((s−i)/s)∗((n−j)/n)((s−i)/s)∗((n−j)/n)
- 那么我们很自然的就得到了下面的转移方程
dp[i][j]=p1∗dp[i][j]+p2∗dp[i][j+1]+p3∗dp[i+1][j]+p4∗dp[i+1][j+1]dp[i][j]=p1∗dp[i][j]+p2∗dp[i][j+1]+p3∗dp[i+1][j]+p4∗dp[i+1][j+1]
代码
1 | <ol class = "linenums" ><li class = "L0" ><span class = "com" >#include</span><span class = "pln" > </span><span class = "str" ><iostream></span></li><li class = "L1" ><span class = "com" >#include</span><span class = "pln" > </span><span class = "str" ><cstdio></span></li><li class = "L2" ><span class = "com" >#include</span><span class = "pln" > </span><span class = "str" ><cstring></span></li><li class = "L3" ><span class = "pln" > </span></li><li class = "L4" ><span class = "kwd" > const </span><span class = "pln" > </span><span class = "kwd" > int </span><span class = "pln" > maxn </span><span class = "pun" >=</span><span class = "pln" > </span><span class = "lit" >1010</span><span class = "pun" >;</span></li><li class = "L5" ><span class = "pln" > </span></li><li class = "L6" ><span class = "kwd" > using </span><span class = "pln" > </span><span class = "kwd" > namespace </span><span class = "pln" > std</span><span class = "pun" >;</span></li><li class = "L7" ><span class = "pln" > </span></li><li class = "L8" ><span class = "kwd" > int </span><span class = "pln" > n</span><span class = "pun" >,</span><span class = "pln" > s</span><span class = "pun" >;</span></li><li class = "L9" ><span class = "kwd" > double </span><span class = "pln" > dp</span><span class = "pun" >[</span><span class = "pln" >maxn</span><span class = "pun" >][</span><span class = "pln" >maxn</span><span class = "pun" >],</span><span class = "pln" > p1</span><span class = "pun" >,</span><span class = "pln" > p2</span><span class = "pun" >,</span><span class = "pln" > p3</span><span class = "pun" >,</span><span class = "pln" > p4</span><span class = "pun" >;</span></li><li class = "L0" ><span class = "pln" > </span></li><li class = "L1" ><span class = "kwd" > int </span><span class = "pln" > main</span><span class = "pun" >()</span><span class = "pln" > </span><span class = "pun" >{</span></li><li class = "L2" ><span class = "pln" > </span><span class = "kwd" > while </span><span class = "pln" > </span><span class = "pun" >(</span><span class = "pln" > scanf </span><span class = "pun" >(</span><span class = "str" > "%d%d" </span><span class = "pun" >,</span><span class = "pln" > </span><span class = "pun" >&</span><span class = "pln" >n</span><span class = "pun" >,</span><span class = "pln" > </span><span class = "pun" >&</span><span class = "pln" >s</span><span class = "pun" >)</span><span class = "pln" > </span><span class = "pun" >==</span><span class = "pln" > </span><span class = "lit" >2</span><span class = "pun" >)</span><span class = "pln" > </span><span class = "pun" >{</span></li><li class = "L3" ><span class = "pln" > memset </span><span class = "pun" >(</span><span class = "pln" >dp</span><span class = "pun" >,</span><span class = "lit" >0</span><span class = "pun" >,</span><span class = "kwd" > sizeof </span><span class = "pun" >(</span><span class = "pln" >dp</span><span class = "pun" >));</span></li><li class = "L4" ><span class = "pln" > </span><span class = "kwd" > for </span><span class = "pun" >(</span><span class = "kwd" > int </span><span class = "pln" > i</span><span class = "pun" >=</span><span class = "pln" >n</span><span class = "pun" >;</span><span class = "pln" > i</span><span class = "pun" >>=</span><span class = "lit" >0</span><span class = "pun" >;</span><span class = "pln" > </span><span class = "pun" >--</span><span class = "pln" >i</span><span class = "pun" >)</span><span class = "pln" > </span><span class = "pun" >{</span></li><li class = "L5" ><span class = "pln" > </span><span class = "kwd" > for </span><span class = "pun" >(</span><span class = "kwd" > int </span><span class = "pln" > j</span><span class = "pun" >=</span><span class = "pln" >s</span><span class = "pun" >;</span><span class = "pln" > j</span><span class = "pun" >>=</span><span class = "lit" >0</span><span class = "pun" >;</span><span class = "pln" > </span><span class = "pun" >--</span><span class = "pln" >j</span><span class = "pun" >)</span><span class = "pln" > </span><span class = "pun" >{</span></li><li class = "L6" ><span class = "pln" > </span><span class = "kwd" > if </span><span class = "pun" >(</span><span class = "pln" >i</span><span class = "pun" >==</span><span class = "pln" >n</span><span class = "pun" >&&</span><span class = "pln" >j</span><span class = "pun" >==</span><span class = "pln" >s</span><span class = "pun" >)</span><span class = "kwd" > continue </span><span class = "pun" >;</span></li><li class = "L7" ><span class = "pln" > p1</span><span class = "pun" >=</span><span class = "lit" >1.0</span><span class = "pun" >*</span><span class = "pln" >i</span><span class = "pun" >/</span><span class = "pln" >n</span><span class = "pun" >*</span><span class = "pln" >j</span><span class = "pun" >/</span><span class = "pln" >s</span><span class = "pun" >;</span></li><li class = "L8" ><span class = "pln" > p2</span><span class = "pun" >=</span><span class = "lit" >1.0</span><span class = "pun" >*(</span><span class = "pln" >n</span><span class = "pun" >-</span><span class = "pln" >i</span><span class = "pun" >)/</span><span class = "pln" >n</span><span class = "pun" >*</span><span class = "pln" >j</span><span class = "pun" >/</span><span class = "pln" >s</span><span class = "pun" >;</span></li><li class = "L9" ><span class = "pln" > p3</span><span class = "pun" >=</span><span class = "lit" >1.0</span><span class = "pun" >*</span><span class = "pln" >i</span><span class = "pun" >/</span><span class = "pln" >n</span><span class = "pun" >*(</span><span class = "pln" >s</span><span class = "pun" >-</span><span class = "pln" >j</span><span class = "pun" >)/</span><span class = "pln" >s</span><span class = "pun" >;</span></li><li class = "L0" ><span class = "pln" > p4</span><span class = "pun" >=</span><span class = "lit" >1.0</span><span class = "pun" >*(</span><span class = "pln" >n</span><span class = "pun" >-</span><span class = "pln" >i</span><span class = "pun" >)/</span><span class = "pln" >n</span><span class = "pun" >*(</span><span class = "pln" >s</span><span class = "pun" >-</span><span class = "pln" >j</span><span class = "pun" >)/</span><span class = "pln" >s</span><span class = "pun" >;</span></li><li class = "L1" ><span class = "pln" > dp</span><span class = "pun" >[</span><span class = "pln" >i</span><span class = "pun" >][</span><span class = "pln" >j</span><span class = "pun" >]=(</span><span class = "pln" >p2</span><span class = "pun" >*</span><span class = "pln" >dp</span><span class = "pun" >[</span><span class = "pln" >i</span><span class = "pun" >+</span><span class = "lit" >1</span><span class = "pun" >][</span><span class = "pln" >j</span><span class = "pun" >]+</span><span class = "pln" >p3</span><span class = "pun" >*</span><span class = "pln" >dp</span><span class = "pun" >[</span><span class = "pln" >i</span><span class = "pun" >][</span><span class = "pln" >j</span><span class = "pun" >+</span><span class = "lit" >1</span><span class = "pun" >]+</span><span class = "pln" >p4</span><span class = "pun" >*</span><span class = "pln" >dp</span><span class = "pun" >[</span><span class = "pln" >i</span><span class = "pun" >+</span><span class = "lit" >1</span><span class = "pun" >][</span><span class = "pln" >j</span><span class = "pun" >+</span><span class = "lit" >1</span><span class = "pun" >]+</span><span class = "lit" >1.0</span><span class = "pun" >)/(</span><span class = "lit" >1.0</span><span class = "pun" >-</span><span class = "pln" >p1</span><span class = "pun" >);</span></li><li class = "L2" ><span class = "pln" > </span><span class = "pun" >}</span></li><li class = "L3" ><span class = "pln" > </span><span class = "pun" >}</span></li><li class = "L4" ><span class = "pln" > printf </span><span class = "pun" >(</span><span class = "str" > "%.4f\n" </span><span class = "pun" >,</span><span class = "pln" >dp</span><span class = "pun" >[</span><span class = "lit" >0</span><span class = "pun" >][</span><span class = "lit" >0</span><span class = "pun" >]);</span></li><li class = "L5" ><span class = "pln" > </span><span class = "pun" >}</span></li><li class = "L6" ><span class = "pun" >}</span></li></ol> |
作者:Mystical-W
来源:http://www.cnblogs.com/bljfy
说明:客官~~您如果觉得写得好的话,记得给我点赞哦。如果要转载的请在合适的地方注明出处。谢
谢您的合作。您要是有什么有疑问的地方可以在下面给我评论。也可以直接私信我哦
声明:本作品由Mystical-W采用知识共享署名-非商业性使用-禁止演绎 4.0 国
际许可协议进行许可
来源:http://www.cnblogs.com/bljfy
说明:客官~~您如果觉得写得好的话,记得给我点赞哦。如果要转载的请在合适的地方注明出处。谢
谢您的合作。您要是有什么有疑问的地方可以在下面给我评论。也可以直接私信我哦
声明:本作品由Mystical-W采用知识共享署名-非商业性使用-禁止演绎 4.0 国
际许可协议进行许可
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· 盘点!HelloGitHub 年度热门开源项目
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· C#使用yield关键字提升迭代性能与效率
· 回顾我的软件开发经历(1)