Typesetting math: 100%

POJ P2096 Collecting Bugs

思路


分类讨论,不妨先设DP[i][j]DP[i][j]表示已经发现ii种子系统中有nnbugbug无非只有四种情况

 

  • 发现的bugbug在旧的系统旧的分类,概率p1p1(i/s)(j/n)(i/s)(j/n).
  • 发现的bugbug在旧的系统新的分类,概率p2p2(i/s)((nj)/n)(i/s)((nj)/n)
  • 发现的bugbug在新的系统旧的分类,概率p3p3((si)/s)(j/n)((si)/s)(j/n)
  • 发现的bugbug在新的系统新的分类,概率p4p4((si)/s)((nj)/n)((si)/s)((nj)/n)
  • 那么我们很自然的就得到了下面的转移方程

dp[i][j]=p1dp[i][j]+p2dp[i][j+1]+p3dp[i+1][j]+p4dp[i+1][j+1]dp[i][j]=p1dp[i][j]+p2dp[i][j+1]+p3dp[i+1][j]+p4dp[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>

  

  

posted @   Mystical-W  阅读(164)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
阅读排行:
· 盘点!HelloGitHub 年度热门开源项目
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· C#使用yield关键字提升迭代性能与效率
· 回顾我的软件开发经历(1)
点击右上角即可分享
微信分享提示