﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-beloved_ACM</title><link>http://www.cppblog.com/cxysteven/</link><description>SB，NB，都是一个B.</description><language>zh-cn</language><lastBuildDate>Thu, 09 Apr 2026 08:47:33 GMT</lastBuildDate><pubDate>Thu, 09 Apr 2026 08:47:33 GMT</pubDate><ttl>60</ttl><item><title>POJ_1200_RK算法</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/11/155550.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Sun, 11 Sep 2011 04:38:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/11/155550.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/155550.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/11/155550.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/155550.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/155550.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;c[</span><span style="color: #000000">1000</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;s[</span><span style="color: #000000">10000000</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;ss[</span><span style="color: #000000">1000</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;key[</span><span style="color: #000000">1000</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;hash[</span><span style="color: #000000">10000000</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,d;<br /><img id="Codehighlighter1_143_927_Open_Image" onclick="this.style.display='none'; Codehighlighter1_143_927_Open_Text.style.display='none'; Codehighlighter1_143_927_Closed_Image.style.display='inline'; Codehighlighter1_143_927_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_143_927_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_143_927_Closed_Text.style.display='none'; Codehighlighter1_143_927_Open_Image.style.display='inline'; Codehighlighter1_143_927_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_143_927_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_143_927_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_187_888_Open_Image" onclick="this.style.display='none'; Codehighlighter1_187_888_Open_Text.style.display='none'; Codehighlighter1_187_888_Closed_Image.style.display='inline'; Codehighlighter1_187_888_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_187_888_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_187_888_Closed_Text.style.display='none'; Codehighlighter1_187_888_Open_Image.style.display='inline'; Codehighlighter1_187_888_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n,</span><span style="color: #000000">&amp;</span><span style="color: #000000">d)</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;EOF)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_187_888_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_187_888_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(hash,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(hash));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%s</span><span style="color: #000000">"</span><span style="color: #000000">,s);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;len&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;strlen(s);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[</span><span style="color: #000000">0</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;c[i</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">*</span><span style="color: #000000">d;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_426_523_Open_Image" onclick="this.style.display='none'; Codehighlighter1_426_523_Open_Text.style.display='none'; Codehighlighter1_426_523_Closed_Image.style.display='inline'; Codehighlighter1_426_523_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_426_523_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_426_523_Closed_Text.style.display='none'; Codehighlighter1_426_523_Open_Image.style.display='inline'; Codehighlighter1_426_523_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;len;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_426_523_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_426_523_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(key[s[i]]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">)&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key[s[i]]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;k</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(k&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;d)&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;h&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img id="Codehighlighter1_585_625_Open_Image" onclick="this.style.display='none'; Codehighlighter1_585_625_Open_Text.style.display='none'; Codehighlighter1_585_625_Closed_Image.style.display='inline'; Codehighlighter1_585_625_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_585_625_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_585_625_Closed_Text.style.display='none'; Codehighlighter1_585_625_Open_Image.style.display='inline'; Codehighlighter1_585_625_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_585_625_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_585_625_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;h</span><span style="color: #000000">*</span><span style="color: #000000">d&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;key[s[i]];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash[h]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;ans&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img id="Codehighlighter1_708_854_Open_Image" onclick="this.style.display='none'; Codehighlighter1_708_854_Open_Text.style.display='none'; Codehighlighter1_708_854_Closed_Image.style.display='inline'; Codehighlighter1_708_854_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_708_854_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_708_854_Closed_Text.style.display='none'; Codehighlighter1_708_854_Open_Image.style.display='inline'; Codehighlighter1_708_854_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;len</span><span style="color: #000000">-</span><span style="color: #000000">n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_708_854_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_708_854_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;d</span><span style="color: #000000">*</span><span style="color: #000000">h&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;key[s[i]]</span><span style="color: #000000">*</span><span style="color: #000000">c[n]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;key[s[i</span><span style="color: #000000">+</span><span style="color: #000000">n]];<br /><img id="Codehighlighter1_783_844_Open_Image" onclick="this.style.display='none'; Codehighlighter1_783_844_Open_Text.style.display='none'; Codehighlighter1_783_844_Closed_Image.style.display='inline'; Codehighlighter1_783_844_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_783_844_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_783_844_Closed_Text.style.display='none'; Codehighlighter1_783_844_Open_Image.style.display='inline'; Codehighlighter1_783_844_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">hash[h])&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_783_844_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_783_844_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash[h]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,ans);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;system("pause");</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><img src ="http://www.cppblog.com/cxysteven/aggbug/155550.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-11 12:38 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/11/155550.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>组合数学之错牌问题</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/11/155549.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Sun, 11 Sep 2011 04:10:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/11/155549.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/155549.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/11/155549.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/155549.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/155549.html</trackback:ping><description><![CDATA[<p style="text-align: center; text-indent: 24pt" align="center"><strong><span style="font-family: 宋体">排列组合专题之错排问题</span></strong></p>
<p style="text-indent: 24pt">1993<span style="font-family: 宋体">年的全国高考试题一改以前数学高考&#8220;以知识立意&#8221;命题思路，开始明确提出&#8220;以能力立意&#8221;，这是数学高考改革的一项重要举措，高考数学命题更加注重了对能力和素质的考查，试题设计增加了应用性和能力型题目，其中的&#8220;贺卡问题&#8221;就属于这方面的一道好题。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">贺卡问题：同室四人各写一张贺年卡，先集中起来，然后每人从中拿一张别人送的贺年卡，则四张不同的贺年卡不同的分配方式有：</span></p>
<p style="text-indent: 24pt">A<span style="font-family: 宋体">、</span>6<span style="font-family: 宋体">种</span>&nbsp;B<span style="font-family: 宋体">、</span>9<span style="font-family: 宋体">种</span>&nbsp;C<span style="font-family: 宋体">、</span>11<span style="font-family: 宋体">种</span>&nbsp;D<span style="font-family: 宋体">、</span>23<span style="font-family: 宋体">种</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">这个问题的情境新颖，无法直接套用公式、法则，主要考查分类或分步计数原理或从反面考虑（排除法）的思想方法，考查分析问题与解决问题的能力，本题以组合数学中著名的&#8220;错排问题&#8221;为背景，用贴近学生身边生活的&#8220;贺卡&#8221;来设计问题，显得较恰当。其实，实际生活中的&#8220;错排问题&#8221;有多种多样，如信封中装错信件或坐错座位等等。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">错排问题：有</span>n<span style="font-family: 宋体">个正整数</span>1<span style="font-family: 宋体">，</span>2<span style="font-family: 宋体">，</span>3<span style="font-family: 宋体">，&#8230;&#8230;</span>n<span style="font-family: 宋体">，将这</span>n<span style="font-family: 宋体">个正整数重新排列，使其中的每一个数都不在原来的位置上，这种排列称为正整数</span>1<span style="font-family: 宋体">，</span>2<span style="font-family: 宋体">，</span>3<span style="font-family: 宋体">，&#8230;&#8230;</span>n<span style="font-family: 宋体">的错排，问这</span>n<span style="font-family: 宋体">个正整数的排个数是多少？</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">设这</span>n<span style="font-family: 宋体">个正整数的错排个数为</span>a<sub>n</sub><span style="font-family: 宋体">，为了探求</span>a<sub>n</sub><span style="font-family: 宋体">的表达式，我们先从最特殊的情形入手。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">当</span>n=1<span style="font-family: 宋体">时，由于只有一个数</span>1<span style="font-family: 宋体">，不可能有错排，所以</span>a<sub>1</sub>=0.</p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">当</span>n=2<span style="font-family: 宋体">时，两个数的错排是唯一的，所以</span>a<sub>2</sub>=1.</p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">当</span>n=3<span style="font-family: 宋体">时，三个数</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">只有</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">和</span>3<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">两种错排，所以</span>a<sub>3</sub>=2.</p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">当</span>n=4<span style="font-family: 宋体">时，四个数</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">的错排有：</span>2<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">；</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">；</span>2<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">；</span>3<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">；</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">；</span>4<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">；</span>4<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">；</span>4<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>1<span style="font-family: 宋体">，共有</span>9<span style="font-family: 宋体">种错排，所以</span>a<sub>4</sub>=9.</p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">刚才提到的</span>93<span style="font-family: 宋体">年高考试题&#8212;&#8212;贺卡问题，实际上求的就是</span>a<sub>4</sub><span style="font-family: 宋体">等于多少？</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">上面使用的是枚举法，当</span>n<span style="font-family: 宋体">较大时，这种方法是很麻烦的、难以解决问题的，必须另辟蹊径，现在考虑用排除法求出</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">这四个正整数的错排的种数，从中摸索出规律。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">对于四个正整数</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">，这四个数的全排列数为</span>4<span style="font-family: 宋体">！。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">有一个数不错排的情况应排除，由于</span>1<span style="font-family: 宋体">排在第</span>1<span style="font-family: 宋体">位的有</span>3<span style="font-family: 宋体">！种，</span>2<span style="font-family: 宋体">排在第</span>2<span style="font-family: 宋体">位的有</span>3<span style="font-family: 宋体">！种，&#8230;&#8230;</span>4<span style="font-family: 宋体">排在第</span>4<span style="font-family: 宋体">位的有</span>3<span style="font-family: 宋体">！种，所以共应排除</span>4<span style="font-family: 宋体">&#215;</span>3<span style="font-family: 宋体">！种。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">然而在排除有一个数不错排的情况时，把同时有两个数不错排的情况也排除了，应予以补上，由于</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">分别排在第</span>1<span style="font-family: 宋体">、第</span>2<span style="font-family: 宋体">位上的情况共有</span>2<span style="font-family: 宋体">！种，同理</span>1<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">分别排在第</span>1<span style="font-family: 宋体">、第</span>3<span style="font-family: 宋体">位上的情况也有</span>2<span style="font-family: 宋体">！种，&#8230;&#8230;，这四个数中同时有两个数不错排的情况共有种，所以应补上</span><span style="position: relative; top: 12pt"> </span><span style="font-family: 宋体">种。</span><span> 
<table cellspacing="0" cellpadding="0" width="100%">
<tbody>
<tr>
<td style="border-bottom: #f0f0f0; border-left: #f0f0f0; background-color: transparent; border-top: #f0f0f0; border-right: #f0f0f0">
<div>
<p>&nbsp;</p></div></td></tr></tbody></table></span><span style="font-family: 宋体">在补上同时有两个数不错排的情况时，把同时有三个数不错排的情况也补上了，应予以排除，四个数中有</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">不错排，</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">不错排，</span>1<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">不错排和</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">不错排共</span> <span style="font-family: 宋体">种情况，所以应排除</span> <span style="font-family: 宋体">种。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">在排除同时有三个数不错排的情况时，把同时有四个数不错排的情况也排除了，所以应补上同时有四个数不错排的情况仅</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">这一种。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">综上所述，</span> &nbsp;</p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">由</span> <span style="font-family: 宋体">以及</span> <span style="font-family: 宋体">，</span> <span style="font-family: 宋体">，我们猜想</span>n<span style="font-family: 宋体">个正整数</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">、&#8230;&#8230;、</span>n<span style="font-family: 宋体">的错排的种数</span>a<sub>n</sub><span style="font-family: 宋体">的表达式为</span>a<sub>n</sub>= <span style="font-family: 宋体">，下面我们来证明这个表达式。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">一般来说，正整数</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、&#8230;&#8230;、</span>n<span style="font-family: 宋体">的全排列有</span>n<span style="font-family: 宋体">！种，其中第</span>k<span style="font-family: 宋体">位是</span>k<span style="font-family: 宋体">的排列有（</span>n-1<span style="font-family: 宋体">）！，当</span>k<span style="font-family: 宋体">取</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、&#8230;&#8230;、</span>n<span style="font-family: 宋体">时，共有</span>n<span style="font-family: 宋体">&#215;（</span>n-1<span style="font-family: 宋体">）！种排列，由于是错排，这些排列应排除，但是此时把同时有两个数不错排的排列多排除了一次，应补上；在补上时，把同时有三个数不错排的排列多补上了一次，应排除；&#8230;&#8230;；继续这一过程，得到错排的排列种数为</span></p>
<p style="margin: 0cm 0cm 0pt 23.95pt"><span style="font-family: 宋体">，</span></p>
<p style="margin: 0cm 0cm 0pt 23.95pt"><span style="font-family: 宋体">，即</span> .</p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">计数是一种常见的数学问题，涉及计数问题的另一种思路是运用递推方法，应该说，利用递推方法是解决计数问题的重要方法，下面我们再用递推关系求</span>a<sub>n</sub><span style="font-family: 宋体">的值。</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">由题意</span>a<sub>1</sub>=0<span style="font-family: 宋体">，</span>a<sub>2</sub>=1<span style="font-family: 宋体">，当</span>n<span style="font-family: 宋体">&#8805;</span>3<span style="font-family: 宋体">时，在错排中</span>n<span style="font-family: 宋体">必不在第</span>n<span style="font-family: 宋体">位，设</span>n<span style="font-family: 宋体">放在第</span>k<span style="font-family: 宋体">位上（</span>1<span style="font-family: 宋体">&#8804;</span>k<span style="font-family: 宋体">&#8804;</span>n-1<span style="font-family: 宋体">），则第</span>n<span style="font-family: 宋体">位上有两种可能：</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">（</span>1<span style="font-family: 宋体">）如果第</span>n<span style="font-family: 宋体">位上不是</span>k<span style="font-family: 宋体">，那么把第</span>n<span style="font-family: 宋体">位看作第</span>k<span style="font-family: 宋体">位，将</span>n<span style="font-family: 宋体">以外的</span>n<span style="font-family: 宋体">－</span>1<span style="font-family: 宋体">个数进行错排，错排个数是</span>a<sub>n-1</sub><span style="font-family: 宋体">；</span></p>
<p style="text-indent: 24pt"><span style="font-family: 宋体">（</span>2<span style="font-family: 宋体">）如果第</span>n<span style="font-family: 宋体">位上是</span>k<span style="font-family: 宋体">，那么除</span>n<span style="font-family: 宋体">和</span>k<span style="font-family: 宋体">以外的</span>n<span style="font-family: 宋体">－</span>2<span style="font-family: 宋体">个数的错排是</span>a<sub>n-2</sub><span style="font-family: 宋体">，</span></p>
<p><span style="font-family: 宋体">所以</span>n<span style="font-family: 宋体">在第</span>k<span style="font-family: 宋体">位上的错排数共有</span>a<sub>n-1</sub><span style="font-family: 宋体">＋</span>a<sub>n-2</sub><span style="font-family: 宋体">，由于</span>k<span style="font-family: 宋体">可取</span>1<span style="font-family: 宋体">、</span>2<span style="font-family: 宋体">、</span>3<span style="font-family: 宋体">、</span>4<span style="font-family: 宋体">、&#8230;&#8230;、</span>n<span style="font-family: 宋体">－</span>1<span style="font-family: 宋体">共</span>n<span style="font-family: 宋体">－</span>1<span style="font-family: 宋体">种取法，所以</span>n<span style="font-family: 宋体">个数的错排个数</span> <span style="font-family: 宋体">，其中</span>n<span style="font-family: 宋体">&#8805;</span>3.</p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">下面我们来求数列</span>{a<sub>n</sub>}<span style="font-family: 宋体">的通项。</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">为了方便起见，设</span> ,</p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">则</span> </p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">即：</span> </p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">于是有</span> <span style="font-family: 宋体">，</span></p>
<p style="text-indent: 21pt"><span style="font-family: 宋体">从而可求</span> <span style="font-family: 宋体">，所以</span> <span style="font-family: 宋体">。</span></p><img src ="http://www.cppblog.com/cxysteven/aggbug/155549.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-11 12:10 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/11/155549.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>POJ_2234_多堆取石子游戏</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/10/155486.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Fri, 09 Sep 2011 17:45:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/10/155486.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/155486.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/10/155486.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/155486.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/155486.html</trackback:ping><description><![CDATA[<p>每堆石子的SG函数就是它自己k.因为他的后继结点可以是0,1,2..k-1,所以此题直接异或即可,为什么异或,参考&lt;&lt;游戏策略&gt;&gt;朱全民的论文</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">vector</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_130_388_Open_Image" onclick="this.style.display='none'; Codehighlighter1_130_388_Open_Text.style.display='none'; Codehighlighter1_130_388_Closed_Image.style.display='inline'; Codehighlighter1_130_388_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_130_388_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_130_388_Closed_Text.style.display='none'; Codehighlighter1_130_388_Open_Image.style.display='inline'; Codehighlighter1_130_388_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_130_388_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_130_388_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a,b,n;<br /><img id="Codehighlighter1_185_344_Open_Image" onclick="this.style.display='none'; Codehighlighter1_185_344_Open_Text.style.display='none'; Codehighlighter1_185_344_Closed_Image.style.display='inline'; Codehighlighter1_185_344_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_185_344_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_185_344_Closed_Text.style.display='none'; Codehighlighter1_185_344_Open_Image.style.display='inline'; Codehighlighter1_185_344_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n)&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;EOF)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_185_344_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_185_344_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;k&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img id="Codehighlighter1_235_282_Open_Image" onclick="this.style.display='none'; Codehighlighter1_235_282_Open_Text.style.display='none'; Codehighlighter1_235_282_Closed_Image.style.display='inline'; Codehighlighter1_235_282_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_235_282_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_235_282_Closed_Text.style.display='none'; Codehighlighter1_235_282_Open_Image.style.display='inline'; Codehighlighter1_235_282_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_235_282_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_235_282_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">a);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k&nbsp;</span><span style="color: #000000">^=</span><span style="color: #000000">&nbsp;a;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(k)&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">Yes</span><span style="color: #000000">"</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">No</span><span style="color: #000000">"</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;system("pause");</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div>
<p><br />&nbsp;</p><img src ="http://www.cppblog.com/cxysteven/aggbug/155486.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-10 01:45 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/10/155486.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]无偏博弈</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/10/155485.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Fri, 09 Sep 2011 17:42:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/10/155485.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/155485.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/10/155485.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/155485.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/155485.html</trackback:ping><description><![CDATA[<div class="article_title"><span class="ico ico_type_Original"></span>
<h3><span class="link_title">转自<a href="http://blog.csdn.net/linulysses/article/details/6341984">http://blog.csdn.net/linulysses/article/details/6341984</a><br /><a title="无偏博弈(Impartial Game) 及几道相关的 POJ 解题报告" href="http://blog.csdn.net/linulysses/article/details/6341984"><font color="#000000"><br />无偏博弈(Impartial Game) 及几道相关的 POJ 解题报告</font></a></span> </h3></div>
<div class="article_content">
<p>在本人的 POJ 1740 解题报告中，已经提到这是一题有关博弈论的题目。现在，就让我们来一窥博弈论中的无偏博弈及其理论，并应用这些知识来解决几道 POJ 题目。本文定位于无偏博弈的入门介绍和其简单应用，因此，博弈定理的证明和详细讨论将被忽略。</p>
<p>&nbsp;</p>
<p><strong>一、什么是无偏博弈？</strong></p>
<p>&nbsp;</p>
<p>通俗地说，博弈就是&#8220;游戏&#8221;，而无偏博弈就是一种任意局势对参与游戏的两个选手来说都是平等的回合制游戏。因此，选手的区别只在于谁先进行第一个回合（即谁是先手）。按此定义，象棋、五子棋等都不是无偏博弈。这是因为，在这些棋类中，对于任意一个局势，有些棋子只能由某一选手移动；例如，在中国象棋中执红的选手在每一回合中只能操作红棋，而不能操作黑棋。</p>
<p>&nbsp;</p>
<p>除了局势对选手平等之外，还有额外的三个要求：</p>
<p>&nbsp;</p>
<p>（1）信息完全公开。这就是说，有关游戏的所有信息对所有选手都是可知的。因此，像扑克之类的游戏，通常都不是无偏博弈，因为，你看不到对手的牌，对手的也看不到你的。</p>
<p>&nbsp;</p>
<p>（2）无随机性。一旦游戏完全设立好，每一人操作都是非随机的。这就排除了大富翁之类的游戏。</p>
<p>&nbsp;</p>
<p>（3）有限步终止。游戏在有限步之内必然终止。这是一条很强的要求。例如，象棋之类的游戏就不满足这条要求。比如，当对手下感情棋时，游戏就可能无限进行下去。</p>
<p>&nbsp;</p>
<p>说了这么多不是无偏博弈的游戏，现在举一个是的例子，而最经典的例子莫过于最简单的取石子游戏，也叫尼姆(Nim)博弈：有两堆石子，石子数目为 m &gt; 0 和 n &gt; 0。两个选手轮流从这些石堆中取走石子。在每一回合中，选手选定一个至少有一个石子的石堆，从该堆中取走至少一个石子。若某选手最后取走所有的石子，则该选手获胜，游戏宣告结束。</p>
<p>&nbsp;</p>
<p>现在我们来验证这是一个无偏博弈。首先，任意局势对选手都是平等的。在任意回合中，如果一个操作或策略（取走x个石子）是被规则允许的（在这里，是 1&lt;= x &lt;= k），则无论该回合是由选手a或选手b进行，该回合的选手都可以采取该操作或策略。其次，信息完全公正。显然，选手采取的任何策略对于对手来说都是可见的。再次，无随机性。这个很显然。最后，有限步终止。因为每一回合中，至少有一个石子被取走，因此，石子的数目一直在减少。这样，最多 m+n 个回合之后，将没有石子留下，游戏结束。</p>
<p>&nbsp;</p>
<p><strong>二、有向无环图游戏</strong></p>
<p>&nbsp;</p>
<p>现在，设想一个有向无环图上（如无特别说明，本系列中的图都是指有向无环图）的棋子移动游戏：在一个有向无环图上，在某个顶点处有一颗棋子。在每一回合中，选手把该棋子沿某一出边移动到相邻的顶点上。当某选手最后移动棋子，从而使棋子处于一个没有出边的顶点上时，该选手获胜，游戏结束。</p>
<p>&nbsp;</p>
<p>之所以介绍这个图上的游戏，是因为，任意一个无偏游戏，等价于一个有向无环图上的棋子游戏：在无偏游戏中，把每一个局势，也即每一个状态，看成是一个有向图上的一个顶点，而把状态间的转移看成一条有向边。由于有限步原则，该图必然是无环图。在图上的对应于游戏最初状态的顶点处放置一颗棋子，采取某个策略把一个状态 x 变成状态 y则相当于把棋子从对应于状态 x 的顶点 u 沿着有向边移动到对应于状态 y 的顶点 v 上。当棋子无法移动时，游戏就结束。由于图没有环，图上的任意路径都是有限长的，因此，棋子在有限步之后必定无法移动，即到达一个没有出边的顶点上。由于这个对应关系，当我们讨论一个无偏游戏时，总同时假定了一个等价的有向无环图上的游戏。因此，我们不再区分状态和顶点这两个概念。</p>
<p>&nbsp;</p>
<p>没有出边的顶点称为 P-态。如果一个状态可以转移到某一个 P-态，则称其为 N-态。如果一个状态只能转移到 N-态的顶点上，则也称为 P-态。在某回合中，如果选手面对的是 P-态，则当对手以最优策略进行游戏时，该选手必败。这是因为，如果该状态没有出边，则游戏结束，选手输掉了游戏；否则，无论他的策略是什么，都只能转移到某个N-态上，从而对手可以接着把该棋子转移到某个 P-态上。因此，P-态也叫必败态（相对于先手而言），N-态为必胜态（同样的，相对于先手而言）。</p>
<p>&nbsp;</p>
<p>一个很自然的问题是，对于任意一个顶点，是否要么为 P-态，要么为 N-态？对于顶点数有限的图，回答是肯定的。证明留给读者，或者回复本文：）</p>
<p>&nbsp;</p>
<p>另外注意，可以存在顶点数无限，甚至不可数的有向无环图游戏，但每一条路径长度却是有限的。例如，考虑如下游戏：两个选手轮流向一个半径为 R 的桌面上摆放半径为 r 的硬币，使得所有的硬币不得有任何部分位于桌面外，且硬币间互不重叠。摆下最后一个硬币的选手获胜。这个游戏中，状态数目是不可数，但路径长度是有限的，最长不过 R<sup><font size="2">2</font></sup>/r<sup><font size="2">2</font></sup>。</p>
<p>&nbsp;</p>
<p>图游戏可以扩展到在多个图上同时进行的游戏，即图游戏的和：设想 n 个图 G<sub><font size="2">1</font></sub>,G<sub><font size="2">2</font></sub>,...,G<sub><font size="2">n</font></sub>，每个图上在某个顶点处放置一颗棋子。在每一回合中，选手选择一个可以移动棋子的图，并且移动该图上的棋子。当某选手进行最后一次移动，从而使得所有的图上，棋子都无法移动，则游戏结束，该选手获胜。这个图游戏用 G=G<sub><font size="2">1&nbsp;</font></sub>x G<sub><font size="2">2&nbsp;</font></sub>x ... G<sub><font size="2">n</font></sub> 表示，并称其为&nbsp;G<sub><font size="2">1</font></sub>,G<sub><font size="2">2</font></sub>,...,G<sub><font size="2">n</font></sub>&nbsp;的和。例如，上述的取石子游戏可以看成是两个图游戏的和，每个子图游戏对应一个石子堆。</p>
<p>&nbsp;</p>
<p><strong>三、Nim 和</strong></p>
<p>&nbsp;</p>
<p>学计算机的读者对异或运算应当时非常熟悉的：1<span>&#8853;1=0</span><span>&#8853;0=</span><span>0，1</span><span>&#8853;0 = 0</span><span>&#8853;1=1。</span><span>&#8853;运算满足交换律和结合律。并且有如下性质：</span></p>
<p>&nbsp;</p>
<p><span>（1）x</span><span>&#8853;y=0 当且仅当 x=y</span></p>
<p><span>（2）x</span><span>&#8853;0=x</span><span>。</span></p>
<p>&nbsp;</p>
<p><span>有一道有关异或运算的很有名的题目：给定一个数组，除了某个数只出奇数次之后，其它的数都出现了偶数次，请找出这个数。答案就是对所有数进行异或运算，结果就是所求的数。这正是因为 x</span><span>&#8853;x=0，x</span><span>&#8853;0=x。</span></p>
<p>&nbsp;</p>
<p><span>异或运算也被称为Nim 和，这是因为它跟尼姆游戏的密切关系。回到前面提到的取石子游戏。两堆石子数目相等的状态为 P-态。这时因为，无论先手从某堆石子中取走多少石子，对手总是可以在另一堆石子上取走相同数目的石子，从而使得两堆石子的数目再次相等。最终，先手将被逼取走某堆中的所有石子，而对手成为最后一个取走石子的选手，从而获胜。巧的是，在 P-态中，两堆石子数目的异或为 0。不过，这绝对不是巧合而已。事实上，我们可以断言，在有任意数目石堆的取石子游戏中，P-态为那些所有堆的石子数目的异或为 0 的那些状态。即，若一共有 m 堆，每堆有 c<sub><font size="2">k</font></sub> 个石子，则当 c<sub><font size="2">1</font></sub></span><span>&#8853;c<sub><font size="2">2</font></sub>...</span><span>&#8853;c<sub><font size="2">m</font></sub>=0 时，状态 (c<sub><font size="2">1</font></sub>,c<sub><font size="2">2</font></sub>,...,c<sub><font size="2">m</font></sub>) 为 P-态。这个结论很美妙，证明也不是很难。实际上，它只是某个更一般的定理的简单推论而已。我们接下来就介绍这个强大的定理。</span></p>
<p>&nbsp;</p>
<p><strong>四、Sprague-Grundy 定理</strong></p>
<p>&nbsp;</p>
<p><span>首先，定义Sprague-Grundy 函数（简称 SG 函数）：在一个图游戏中，定义一个顶点到非负整数的函数 SG 如下</span></p>
<p>&nbsp;</p>
<p><span>&nbsp;&nbsp; &nbsp; &nbsp; SG(u)= 最小不属于整数集合 {SG(v) | v 是 u 的后继（即有一条边从 u 指向 v）}的非负整数。</span></p>
<p>&nbsp;</p>
<p><span>若用 F(u) 表示顶点 u 的所有后继的集合，即 F(u) = { v | 有一条边从 u 指向 v }，用 mex(S) (minimal exclusive) 表示最小的不属于集合 S 的非负整数，则 SG(u) = mex( F(u) )。</span></p>
<p>&nbsp;</p>
<p><span>例如，在取石子游戏中，假定只有一堆石子，石子数目为 n。则 F(k)=k，0&lt;=k&lt;=n。特别的 F(n)=n。简单的思考可以证明，状态为 P-态当且仅当其 SG 值为0，为 N-态当且仅当 SG 值大于0。</span></p>
<p>&nbsp;</p>
<p>著名的Sprague-Grundy 定理断言，对于有限图游戏 G=G<sub><font size="2">1&nbsp;</font></sub>x G<sub><font size="2">2&nbsp;</font></sub>x ... x G<sub><font size="2">n</font></sub> 上的顶点 (u<sub><font size="2">1</font></sub>,u<sub><font size="2">2</font></sub>,...,u<sub><font size="2">n</font></sub>), 其 SG 值为其子图游戏各顶点的 SG 值之 Nim 和，即 SG(u<sub><font size="2">1</font></sub>,u<sub><font size="2">2</font></sub>,...,u<sub><font size="2">n</font></sub>)=SG(u<sub><font size="2">1</font></sub>)&#8853;SG(u<sub><font size="2">2</font></sub>)&#8853;...&#8853;SG(u<sub><font size="2">n</font></sub>)。证明不难，从略。</p>
<p>&nbsp;</p>
<p>有了这个定理，通过把游戏分解成多个图游戏的和，就可以把复杂的 P-态 判定问题转换成南计算简单子图游戏的 SG 值和子图间顶点 SG 值的 Nim 和，从而使很多游戏的分析变得简单起来。例如，考虑有 n 个堆的取石子游戏。每个堆可以看成一个图游戏，n 个堆上的取石子游戏就成了这些子图游戏的和。则状态 (c<sub><font size="2">1</font></sub>,c<sub><font size="2">2</font></sub>,...,c<sub><font size="2">n</font></sub>) 的 SG 值为 SG(c<sub><font size="2">1</font></sub>)&#8853;SG(c<sub><font size="2">2</font></sub>)&#8853;...&#8853;SG(c<sub><font size="2">n</font></sub>)=c1&#8853;c2&#8853;...&#8853;c<sub><font size="2">n</font></sub>。这也验证了第三节中的断言。</p>
<p>&nbsp;</p>
<p>SG 函数的计算并不难，应用深度优先搜索即可。</p>
<p><textarea class="cpp" rows="15" cols="50" name="code">// procedure to compute SG value for vertex u
function SG(u)
    set F to be empty set
    for each v who is a follower of u do
         add SG(v) to the set F
    end for
    return mex(F)
end function
</textarea>&nbsp;</p>
<p>在实际的代码实现中，计算好的 SG 通常被记录起来，从而避免重复运算。</p>
<p>&nbsp;</p>
<p>基础的无偏博弈就先介绍到这里。对此问题感兴趣的读者，包括定理的证明，本人推荐 UCLA 的 Ferguson 教授写的一篇讲稿（<a href="http://www.cs.cmu.edu/afs/cs/academic/class/15859-f01/www/notes/comb.pdf"><font color="#336699">点击这里</font></a>）。这篇讲稿通俗易懂，还带有练习，是不错的无偏博弈入门读物。</p>
<p>&nbsp;</p>
<p><strong>五、应用</strong></p>
<p>&nbsp;</p>
<p>现在就让我们用这个强大的武器来解决一些原先看起来很难现在很水的 POJ 题目。</p>
<p>&nbsp;</p>
<p>（1）POJ 2234 (Matches Game)。这个就是有多个堆的取石子游戏。直接应用定理即可。</p>
<p>&nbsp;</p>
<p>（2）POJ 2425 (A Chess Game)。这个也是定理的直接应用，只不过这次是直接应用于有向图上而已。计算出每个图的初态的 SG 值，然后取 Nim 和即可判断是否为 P-态。</p>
<p>&nbsp;</p>
<p>（3）POJ 2960 (S-Nim)。这个是更一般的取石子游戏。每次取的石子的可能数目被限定于几个整数，而不是任意整数。解题思路和 (2) 相同，都是要求每个堆上的初态的 SG 值。</p>
<p>&nbsp;</p>
<p>（4）POJ 1704 (Goergia and Bob)。这题也是一个无偏博弈，可以直接通过建立相应的图来求 SG 值。不过，这样一来，这个图会很大，计算量也不少。更好的做法是把游戏分解成多个游戏，使得每个子游戏的 SG 值能够容易计算出，最后应用定理求出原来图的初态的 SG 值，从而判断是否为 P-态。那么，最关键的就是如何分解游戏了。观察到一个棋子是否可以移动取决于它左边相邻的棋子，我们可以从最右边开始，把两个相邻的棋子分成一组，若棋子的有奇数个，则在棋盘的最左边设置一个虚拟的棋子。这样，每组棋子看成一个单独的游戏，其终态就是两个棋子紧挨在一起。从这个角度看，每个子游戏就是一个石堆，于是，每个子游戏就是一个取石子游戏，石子的数目等于两颗棋子之间的空格数目；而总的游戏就是有多个堆的取石子游戏。</p>
<p>&nbsp;</p>
<p>（5）POJ 2348 (Euclid's Game)。这个是著名的经典 Euclid 游戏的一个变体。把每个状态看成图上的顶点，可以发现，如果顶点的出度至少为2，更从该状态出发，可以存在一条长度为奇数的路径到达终态或另一个出度至少也为2的顶点，并且这条路径上的顶点，除了端点外，出度都为1。因此，出度至少为2的顶点都是 N-态。出度为0的显然是终态。出度为1的顶点，如果它到最近的N-态的路径长度为偶数，则为 N-态；否则为 P-态。这样，问题就解决了。不过，这个问题存在更简单的解决办法。它 P-态和经典的 Euclid 游戏的 P-态存在极其简单的关系：经典的 Euclid 游戏中的 P-态，除了两数相等这一情况外，都是本题游戏中的 P-态。而经典的 Euclid 游戏中的 SG 函数，存在简单的解析解，这个解和黄金分割有关。详细见 《<a href="http://www.google.ca/url?sa=t&amp;source=web&amp;cd=2&amp;ved=0CCIQFjAB&amp;url=http%3A%2F%2Fciteseerx.ist.psu.edu%2Fviewdoc%2Fdownload%3Fdoi%3D10.1.1.109.1841%26rep%3Drep1%26type%3Dpdf&amp;ei=DRayTeu4LKXYiAKylsivBg&amp;usg=AFQjCNGZPBcsKFEND3PrdNjIkFx08SCaqA&amp;sig2=ZHZvxw_3OzE-OJEu6BDKUQ"><font color="#336699">The Sprague-Grundy function of the game Euclid</font></a>》(<span class="f">Nivasch</span>&nbsp;-&nbsp;<span class="f">2004)一文。</span></p>
<p>&nbsp;</p>
<p>（6）POJ 2975 (Nim)。这道题也是一道经典的多堆取石子游戏。和题 2234 所不同的是，它不是要判定某个状态是 P-态还是 N-态，而要求获胜策略的数量。设初始状态的 SG 值为 s，则 s 的 MSB (most significant bit) 是指 s 的二进制表示中最左边的非0位。设 s 的 MSB是 x。可以证明，获胜策略的数量是所有堆的 SG 值中，第 x 位非 0 的 SG 值的个数。</p>
<p>&nbsp;</p>
<p>（7）POJ 1082 (Calendar Game)。这题也可以用 SG 函数来分析，不过存在更简单的解决办法。注意到一般情况下，移动一天，或移动一个月，都将改变月或日的奇偶性，例外的情况发生在每个月的最后一天。现在先假定这些例外的日期不能被移动到。由于目标日期 11 月 4 日 是奇数月，偶数日，即奇偶不同，如果一个状态是奇奇或偶偶的情况，选手总可以移动一天或一个月转移到奇偶或偶奇（统称为奇偶）的状态上。而处于奇偶状态时，只能移动到奇奇或偶偶的日期上，从而永远也不可能到达终态。因此，奇偶为 P-态，奇奇或偶偶为 N-态。现在考虑例外的边界情况。逐个仔细研究可以发现，只有 9.30 和 11.30是两个例外。这两个奇偶的状态竟然也可以转到奇偶的状态上！因此，他们也是 N-态。至此，问题解决。</p>
<p>&nbsp;</p>
<p>（8）POJ 1067 (取石子)。经典 Wythoff 博弈，通解和黄金分割有关。</p>
<p>&nbsp;</p>
<p>（9）... ...</p></div><img src ="http://www.cppblog.com/cxysteven/aggbug/155485.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-10 01:42 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/10/155485.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU_2955_ROBBERY</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/09/155443.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Fri, 09 Sep 2011 05:47:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/09/155443.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/155443.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/09/155443.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/155443.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/155443.html</trackback:ping><description><![CDATA[总结:可以从这里知道,0-1背包问题,价值和重量可以交换,对于其中只要一个为整型就可以解决
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;MAXN&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">110</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;f[</span><span style="color: #000000">100000</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;w[MAXN];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;v[MAXN];<br /><img id="Codehighlighter1_160_802_Open_Image" onclick="this.style.display='none'; Codehighlighter1_160_802_Open_Text.style.display='none'; Codehighlighter1_160_802_Closed_Image.style.display='inline'; Codehighlighter1_160_802_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_160_802_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_160_802_Closed_Text.style.display='none'; Codehighlighter1_160_802_Open_Image.style.display='inline'; Codehighlighter1_160_802_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_160_802_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_160_802_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;cas;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">cas);<br /><img id="Codehighlighter1_219_758_Open_Image" onclick="this.style.display='none'; Codehighlighter1_219_758_Open_Text.style.display='none'; Codehighlighter1_219_758_Closed_Image.style.display='inline'; Codehighlighter1_219_758_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_219_758_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_219_758_Closed_Text.style.display='none'; Codehighlighter1_219_758_Open_Image.style.display='inline'; Codehighlighter1_219_758_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(cas</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_219_758_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_219_758_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;p;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%lf%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">p,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;sum&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img id="Codehighlighter1_335_405_Open_Image" onclick="this.style.display='none'; Codehighlighter1_335_405_Open_Text.style.display='none'; Codehighlighter1_335_405_Closed_Image.style.display='inline'; Codehighlighter1_335_405_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_335_405_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_335_405_Closed_Text.style.display='none'; Codehighlighter1_335_405_Open_Image.style.display='inline'; Codehighlighter1_335_405_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_335_405_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_335_405_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%lf</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">w[i],</span><span style="color: #000000">&amp;</span><span style="color: #000000">v[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;w[i];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(f,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(f));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[</span><span style="color: #000000">0</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_546_606_Open_Image" onclick="this.style.display='none'; Codehighlighter1_546_606_Open_Text.style.display='none'; Codehighlighter1_546_606_Closed_Image.style.display='inline'; Codehighlighter1_546_606_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_546_606_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_546_606_Closed_Text.style.display='none'; Codehighlighter1_546_606_Open_Image.style.display='inline'; Codehighlighter1_546_606_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;sum;&nbsp;j&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">&nbsp;w[i];&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_546_606_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_546_606_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;max(f[j],f[j</span><span style="color: #000000">-</span><span style="color: #000000">w[i]]</span><span style="color: #000000">*</span><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000">-</span><span style="color: #000000">v[i]));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000">&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_654_752_Open_Image" onclick="this.style.display='none'; Codehighlighter1_654_752_Open_Text.style.display='none'; Codehighlighter1_654_752_Closed_Image.style.display='inline'; Codehighlighter1_654_752_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_654_752_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_654_752_Closed_Text.style.display='none'; Codehighlighter1_654_752_Open_Image.style.display='inline'; Codehighlighter1_654_752_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;sum;&nbsp;j&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_654_752_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_654_752_Open_Text"><span style="color: #000000">{<br /><img id="Codehighlighter1_683_743_Open_Image" onclick="this.style.display='none'; Codehighlighter1_683_743_Open_Text.style.display='none'; Codehighlighter1_683_743_Closed_Image.style.display='inline'; Codehighlighter1_683_743_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_683_743_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_683_743_Closed_Text.style.display='none'; Codehighlighter1_683_743_Open_Image.style.display='inline'; Codehighlighter1_683_743_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(f[j]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;p)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_683_743_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_683_743_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">j</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;system("pause");</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br /><img src ="http://www.cppblog.com/cxysteven/aggbug/155443.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-09 13:47 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/09/155443.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Dragon_Balls_简单并查集</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/09/155427.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Fri, 09 Sep 2011 03:39:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/09/155427.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/155427.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/09/155427.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/155427.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/155427.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstring</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">string</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">queue</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">vector</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;MAXN&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10010</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n,m;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;father[MAXN],&nbsp;rank[MAXN],&nbsp;a,b;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;depth[MAXN],t;<br /><img id="Codehighlighter1_271_377_Open_Image" onclick="this.style.display='none'; Codehighlighter1_271_377_Open_Text.style.display='none'; Codehighlighter1_271_377_Closed_Image.style.display='inline'; Codehighlighter1_271_377_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_271_377_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_271_377_Closed_Text.style.display='none'; Codehighlighter1_271_377_Open_Image.style.display='inline'; Codehighlighter1_271_377_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;make()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_271_377_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_271_377_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_310_375_Open_Image" onclick="this.style.display='none'; Codehighlighter1_310_375_Open_Text.style.display='none'; Codehighlighter1_310_375_Closed_Image.style.display='inline'; Codehighlighter1_310_375_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_310_375_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_310_375_Closed_Text.style.display='none'; Codehighlighter1_310_375_Open_Image.style.display='inline'; Codehighlighter1_310_375_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_310_375_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_310_375_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;father[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;i;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rank[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;depth[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_397_694_Open_Image" onclick="this.style.display='none'; Codehighlighter1_397_694_Open_Text.style.display='none'; Codehighlighter1_397_694_Closed_Image.style.display='inline'; Codehighlighter1_397_694_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_397_694_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_397_694_Closed_Text.style.display='none'; Codehighlighter1_397_694_Open_Image.style.display='inline'; Codehighlighter1_397_694_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;_find(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_397_694_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_397_694_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;h&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;x;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img id="Codehighlighter1_456_517_Open_Image" onclick="this.style.display='none'; Codehighlighter1_456_517_Open_Text.style.display='none'; Codehighlighter1_456_517_Closed_Image.style.display='inline'; Codehighlighter1_456_517_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_456_517_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_456_517_Closed_Text.style.display='none'; Codehighlighter1_456_517_Open_Image.style.display='inline'; Codehighlighter1_456_517_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(h&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;father[h])&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_456_517_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_456_517_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;t&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;depth[h];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;father[h];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;y;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;tmp;<br /><img id="Codehighlighter1_570_678_Open_Image" onclick="this.style.display='none'; Codehighlighter1_570_678_Open_Text.style.display='none'; Codehighlighter1_570_678_Closed_Image.style.display='inline'; Codehighlighter1_570_678_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_570_678_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_570_678_Closed_Text.style.display='none'; Codehighlighter1_570_678_Open_Image.style.display='inline'; Codehighlighter1_570_678_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(x&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;h)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_570_678_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_570_678_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;</span><span style="color: #000000">-=</span><span style="color: #000000">&nbsp;depth[x];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;depth[x]&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;t;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;father[x];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;father[x]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;h;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;y;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;h;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_722_852_Open_Image" onclick="this.style.display='none'; Codehighlighter1_722_852_Open_Text.style.display='none'; Codehighlighter1_722_852_Closed_Image.style.display='inline'; Codehighlighter1_722_852_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_722_852_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_722_852_Closed_Text.style.display='none'; Codehighlighter1_722_852_Open_Image.style.display='inline'; Codehighlighter1_722_852_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;_union(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;b)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_722_852_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_722_852_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;_find(a);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;y&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;_find(b);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rank[y]&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;rank[x];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;depth[x]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;father[x]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;y;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;type;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_878_1550_Open_Image" onclick="this.style.display='none'; Codehighlighter1_878_1550_Open_Text.style.display='none'; Codehighlighter1_878_1550_Closed_Image.style.display='inline'; Codehighlighter1_878_1550_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_878_1550_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_878_1550_Closed_Text.style.display='none'; Codehighlighter1_878_1550_Open_Image.style.display='inline'; Codehighlighter1_878_1550_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_878_1550_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_878_1550_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;cas;<br /><img id="Codehighlighter1_933_1502_Open_Image" onclick="this.style.display='none'; Codehighlighter1_933_1502_Open_Text.style.display='none'; Codehighlighter1_933_1502_Closed_Image.style.display='inline'; Codehighlighter1_933_1502_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_933_1502_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_933_1502_Closed_Text.style.display='none'; Codehighlighter1_933_1502_Open_Image.style.display='inline'; Codehighlighter1_933_1502_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">cas)&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;EOF)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_933_1502_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_933_1502_Open_Text"><span style="color: #000000">{<br /><img id="Codehighlighter1_980_1496_Open_Image" onclick="this.style.display='none'; Codehighlighter1_980_1496_Open_Text.style.display='none'; Codehighlighter1_980_1496_Closed_Image.style.display='inline'; Codehighlighter1_980_1496_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_980_1496_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_980_1496_Closed_Text.style.display='none'; Codehighlighter1_980_1496_Open_Image.style.display='inline'; Codehighlighter1_980_1496_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;acase&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;acase&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;cas;&nbsp;acase</span><span style="color: #000000">++</span><span style="color: #000000">)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_980_1496_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_980_1496_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n,</span><span style="color: #000000">&amp;</span><span style="color: #000000">m);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">Case&nbsp;%d:\n</span><span style="color: #000000">"</span><span style="color: #000000">,acase);</span><span style="color: #008000">//</span><span style="color: #008000">&lt;&lt;":"&lt;&lt;endl;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;make();<br /><img id="Codehighlighter1_1109_1490_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1109_1490_Open_Text.style.display='none'; Codehighlighter1_1109_1490_Closed_Image.style.display='inline'; Codehighlighter1_1109_1490_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1109_1490_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1109_1490_Closed_Text.style.display='none'; Codehighlighter1_1109_1490_Open_Image.style.display='inline'; Codehighlighter1_1109_1490_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;m;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1109_1490_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1109_1490_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">\n%c</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">type);<br /><img id="Codehighlighter1_1180_1265_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1180_1265_Open_Text.style.display='none'; Codehighlighter1_1180_1265_Closed_Image.style.display='inline'; Codehighlighter1_1180_1265_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1180_1265_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1180_1265_Closed_Text.style.display='none'; Codehighlighter1_1180_1265_Open_Image.style.display='inline'; Codehighlighter1_1180_1265_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(type&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">'</span><span style="color: #000000">T</span><span style="color: #000000">'</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1180_1265_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1180_1265_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">a,</span><span style="color: #000000">&amp;</span><span style="color: #000000">b);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_union(a,b);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_1282_1481_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1282_1481_Open_Text.style.display='none'; Codehighlighter1_1282_1481_Closed_Image.style.display='inline'; Codehighlighter1_1282_1481_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1282_1481_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1282_1481_Closed_Text.style.display='none'; Codehighlighter1_1282_1481_Open_Image.style.display='inline'; Codehighlighter1_1282_1481_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1282_1481_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1282_1481_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">a);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;tmp&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;_find(a);&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%d&nbsp;%d&nbsp;%d\n</span><span style="color: #000000">"</span><span style="color: #000000">,tmp,rank[tmp],depth[a]);&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;cout&lt;&lt;tmp&lt;&lt;"&nbsp;"&lt;&lt;rank[tmp]&lt;&lt;"&nbsp;"&lt;&lt;depth[a]&lt;&lt;endl;;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">system("pause");</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><img src ="http://www.cppblog.com/cxysteven/aggbug/155427.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-09 11:39 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/09/155427.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>最优比例生成树</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/09/155404.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Thu, 08 Sep 2011 20:27:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/09/155404.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/155404.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/09/155404.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/155404.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/155404.html</trackback:ping><description><![CDATA[<div>最优比例生成树,分数规划
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #008000">//</span><span style="color: #008000">0/1分数规划满足单调性，也就是比例生成树已经确定了，只要去猜l，让他满足Z（l）等于0&nbsp;</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;eps&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;1e</span><span style="color: #000000">-</span><span style="color: #000000">6</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;n;<br /><img id="Codehighlighter1_189_205_Open_Image" onclick="this.style.display='none'; Codehighlighter1_189_205_Open_Text.style.display='none'; Codehighlighter1_189_205_Closed_Image.style.display='inline'; Codehighlighter1_189_205_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_189_205_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_189_205_Closed_Text.style.display='none'; Codehighlighter1_189_205_Open_Image.style.display='inline'; Codehighlighter1_189_205_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;node&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_189_205_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_189_205_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;x,y,z;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000">v[</span><span style="color: #000000">1010</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;g[</span><span style="color: #000000">1010</span><span style="color: #000000">][</span><span style="color: #000000">1010</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;opt[</span><span style="color: #000000">1010</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;vis[</span><span style="color: #000000">1010</span><span style="color: #000000">];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_286_838_Open_Image" onclick="this.style.display='none'; Codehighlighter1_286_838_Open_Text.style.display='none'; Codehighlighter1_286_838_Closed_Image.style.display='inline'; Codehighlighter1_286_838_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_286_838_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_286_838_Closed_Text.style.display='none'; Codehighlighter1_286_838_Open_Image.style.display='inline'; Codehighlighter1_286_838_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;prim()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_286_838_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_286_838_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;minn;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;memset(vis,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(vis));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;vex&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;ans&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0.0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;opt[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;g[</span><span style="color: #000000">1</span><span style="color: #000000">][i];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;vis[</span><span style="color: #000000">1</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img id="Codehighlighter1_472_821_Open_Image" onclick="this.style.display='none'; Codehighlighter1_472_821_Open_Text.style.display='none'; Codehighlighter1_472_821_Closed_Image.style.display='inline'; Codehighlighter1_472_821_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_472_821_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_472_821_Closed_Text.style.display='none'; Codehighlighter1_472_821_Open_Image.style.display='inline'; Codehighlighter1_472_821_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_472_821_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_472_821_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minn&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;1e9</span><span style="color: #000000">+</span><span style="color: #000000">1.0</span><span style="color: #000000">;<br /><img id="Codehighlighter1_530_637_Open_Image" onclick="this.style.display='none'; Codehighlighter1_530_637_Open_Text.style.display='none'; Codehighlighter1_530_637_Closed_Image.style.display='inline'; Codehighlighter1_530_637_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_530_637_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_530_637_Closed_Text.style.display='none'; Codehighlighter1_530_637_Open_Image.style.display='inline'; Codehighlighter1_530_637_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;j</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_530_637_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_530_637_Open_Text"><span style="color: #000000">{<br /><img id="Codehighlighter1_570_629_Open_Image" onclick="this.style.display='none'; Codehighlighter1_570_629_Open_Text.style.display='none'; Codehighlighter1_570_629_Closed_Image.style.display='inline'; Codehighlighter1_570_629_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_570_629_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_570_629_Closed_Text.style.display='none'; Codehighlighter1_570_629_Open_Image.style.display='inline'; Codehighlighter1_570_629_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(opt[j]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;minn&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">!</span><span style="color: #000000">vis[j])&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_570_629_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_570_629_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minn&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;opt[j];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vex&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;j;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vis[vex]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ans&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;minn;<br /><img id="Codehighlighter1_715_816_Open_Image" onclick="this.style.display='none'; Codehighlighter1_715_816_Open_Text.style.display='none'; Codehighlighter1_715_816_Closed_Image.style.display='inline'; Codehighlighter1_715_816_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_715_816_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_715_816_Closed_Text.style.display='none'; Codehighlighter1_715_816_Open_Image.style.display='inline'; Codehighlighter1_715_816_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;j</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_715_816_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_715_816_Open_Text"><span style="color: #000000">{<br /><img id="Codehighlighter1_738_808_Open_Image" onclick="this.style.display='none'; Codehighlighter1_738_808_Open_Text.style.display='none'; Codehighlighter1_738_808_Closed_Image.style.display='inline'; Codehighlighter1_738_808_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_738_808_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_738_808_Closed_Text.style.display='none'; Codehighlighter1_738_808_Open_Image.style.display='inline'; Codehighlighter1_738_808_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">vis[j])&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_738_808_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_738_808_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opt[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;opt[j]&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;g[vex][j]</span><span style="color: #000000">?</span><span style="color: #000000">opt[j]:g[vex][j];&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;ans;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img id="Codehighlighter1_853_1686_Open_Image" onclick="this.style.display='none'; Codehighlighter1_853_1686_Open_Text.style.display='none'; Codehighlighter1_853_1686_Closed_Image.style.display='inline'; Codehighlighter1_853_1686_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_853_1686_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_853_1686_Closed_Text.style.display='none'; Codehighlighter1_853_1686_Open_Image.style.display='inline'; Codehighlighter1_853_1686_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_853_1686_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_853_1686_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_889_1642_Open_Image" onclick="this.style.display='none'; Codehighlighter1_889_1642_Open_Text.style.display='none'; Codehighlighter1_889_1642_Closed_Image.style.display='inline'; Codehighlighter1_889_1642_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_889_1642_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_889_1642_Closed_Text.style.display='none'; Codehighlighter1_889_1642_Open_Image.style.display='inline'; Codehighlighter1_889_1642_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n)&nbsp;</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">&nbsp;n)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_889_1642_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_889_1642_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">&nbsp;;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[i][j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;1e9;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">v[i].x,</span><span style="color: #000000">&amp;</span><span style="color: #000000">v[i].y,</span><span style="color: #000000">&amp;</span><span style="color: #000000">v[i].z);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;l&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,r&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1000000</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;mid;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;dis,cost;<br /><img id="Codehighlighter1_1179_1603_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1179_1603_Open_Text.style.display='none'; Codehighlighter1_1179_1603_Closed_Image.style.display='inline'; Codehighlighter1_1179_1603_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1179_1603_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1179_1603_Closed_Text.style.display='none'; Codehighlighter1_1179_1603_Open_Image.style.display='inline'; Codehighlighter1_1179_1603_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(r&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;l&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">&nbsp;eps)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1179_1603_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1179_1603_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mid&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(l</span><span style="color: #000000">+</span><span style="color: #000000">r)</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_1285_1511_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1285_1511_Open_Text.style.display='none'; Codehighlighter1_1285_1511_Closed_Image.style.display='inline'; Codehighlighter1_1285_1511_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1285_1511_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1285_1511_Closed_Text.style.display='none'; Codehighlighter1_1285_1511_Open_Image.style.display='inline'; Codehighlighter1_1285_1511_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;j&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;j</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1285_1511_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1285_1511_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dis&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;sqrt(</span><span style="color: #000000">1.0</span><span style="color: #000000">*</span><span style="color: #000000">(v[i].x&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;v[j].x)</span><span style="color: #000000">*</span><span style="color: #000000">(v[i].x&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;v[j].x)&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1.0</span><span style="color: #000000">*</span><span style="color: #000000">(v[i].y&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;v[j].y)</span><span style="color: #000000">*</span><span style="color: #000000">(v[i].y&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;v[j].y));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cost&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;fabs(</span><span style="color: #000000">1.0</span><span style="color: #000000">*</span><span style="color: #000000">(v[i].z&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;v[j].z));<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;g[i][j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;g[j][i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;cost&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">&nbsp;mid</span><span style="color: #000000">*</span><span style="color: #000000">dis;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(prim()&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0.0</span><span style="color: #000000">)<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;mid;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000">&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;l&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;mid;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">%.3f\n</span><span style="color: #000000">"</span><span style="color: #000000">,r);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;system("pause");</span><span style="color: #008000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><br /><br /></div><img src ="http://www.cppblog.com/cxysteven/aggbug/155404.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-09 04:27 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/09/155404.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HDU_3416_In_ACTION_最短路+DP</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/08/155349.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Thu, 08 Sep 2011 05:21:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/08/155349.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/155349.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/08/155349.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/155349.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/155349.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">queue</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img id="Codehighlighter1_88_107_Open_Image" onclick="this.style.display='none'; Codehighlighter1_88_107_Open_Text.style.display='none'; Codehighlighter1_88_107_Closed_Image.style.display='inline'; Codehighlighter1_88_107_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_88_107_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_88_107_Closed_Text.style.display='none'; Codehighlighter1_88_107_Open_Image.style.display='inline'; Codehighlighter1_88_107_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">struct</span><span style="color: #000000">&nbsp;Edge&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_88_107_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_88_107_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v,w,next;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">edge[</span><span style="color: #000000">100010</span><span style="color: #000000">];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;INF&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0x3fffffff</span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;sps,head[</span><span style="color: #000000">101</span><span style="color: #000000">],w[</span><span style="color: #000000">101</span><span style="color: #000000">],dist[</span><span style="color: #000000">101</span><span style="color: #000000">];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;inq[</span><span style="color: #000000">101</span><span style="color: #000000">],n,m;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;f[</span><span style="color: #000000">100001</span><span style="color: #000000">];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img id="Codehighlighter1_255_360_Open_Image" onclick="this.style.display='none'; Codehighlighter1_255_360_Open_Text.style.display='none'; Codehighlighter1_255_360_Closed_Image.style.display='inline'; Codehighlighter1_255_360_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_255_360_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_255_360_Closed_Text.style.display='none'; Codehighlighter1_255_360_Open_Image.style.display='inline'; Codehighlighter1_255_360_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;addedge(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;u,</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v,&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;w)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_255_360_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_255_360_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edge[sps].v&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;v;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edge[sps].w&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;w;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;edge[sps].next&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;head[u]<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;head[u]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;sps</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img id="Codehighlighter1_375_906_Open_Image" onclick="this.style.display='none'; Codehighlighter1_375_906_Open_Text.style.display='none'; Codehighlighter1_375_906_Closed_Image.style.display='inline'; Codehighlighter1_375_906_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_375_906_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_375_906_Closed_Text.style.display='none'; Codehighlighter1_375_906_Open_Image.style.display='inline'; Codehighlighter1_375_906_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;spfa()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_375_906_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_375_906_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_412_449_Open_Image" onclick="this.style.display='none'; Codehighlighter1_412_449_Open_Text.style.display='none'; Codehighlighter1_412_449_Closed_Image.style.display='inline'; Codehighlighter1_412_449_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_412_449_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_412_449_Closed_Text.style.display='none'; Codehighlighter1_412_449_Open_Image.style.display='inline'; Codehighlighter1_412_449_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_412_449_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_412_449_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;((i</span><span style="color: #000000">==</span><span style="color: #000000">0</span><span style="color: #000000">)</span><span style="color: #000000">?</span><span style="color: #000000">0</span><span style="color: #000000">:INF);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;queue</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;q;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;q.push(</span><span style="color: #000000">0</span><span style="color: #000000">);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;memset(inq,</span><span style="color: #000000">0</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(inq));<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_537_904_Open_Image" onclick="this.style.display='none'; Codehighlighter1_537_904_Open_Text.style.display='none'; Codehighlighter1_537_904_Closed_Image.style.display='inline'; Codehighlighter1_537_904_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_537_904_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_537_904_Closed_Text.style.display='none'; Codehighlighter1_537_904_Open_Image.style.display='inline'; Codehighlighter1_537_904_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">q.empty())&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_537_904_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_537_904_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;u&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;q.front();q.pop();<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inq[u]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /><img id="Codehighlighter1_658_899_Open_Image" onclick="this.style.display='none'; Codehighlighter1_658_899_Open_Text.style.display='none'; Codehighlighter1_658_899_Closed_Image.style.display='inline'; Codehighlighter1_658_899_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_658_899_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_658_899_Closed_Text.style.display='none'; Codehighlighter1_658_899_Open_Image.style.display='inline'; Codehighlighter1_658_899_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;addr&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;head[u];addr&nbsp;</span><span style="color: #000000">!=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;addr&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;edge[addr].next)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_658_899_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_658_899_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;v&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;edge[addr].v;<br /><img id="Codehighlighter1_737_891_Open_Image" onclick="this.style.display='none'; Codehighlighter1_737_891_Open_Text.style.display='none'; Codehighlighter1_737_891_Closed_Image.style.display='inline'; Codehighlighter1_737_891_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_737_891_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_737_891_Closed_Text.style.display='none'; Codehighlighter1_737_891_Open_Image.style.display='inline'; Codehighlighter1_737_891_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(dist[v]&nbsp;</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;dist[u]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;edge[addr].w)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_737_891_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_737_891_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dist[v]&nbsp;&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;dist[u]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;edge[addr].w;<br /><img id="Codehighlighter1_810_880_Open_Image" onclick="this.style.display='none'; Codehighlighter1_810_880_Open_Text.style.display='none'; Codehighlighter1_810_880_Closed_Image.style.display='inline'; Codehighlighter1_810_880_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_810_880_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_810_880_Closed_Text.style.display='none'; Codehighlighter1_810_880_Open_Image.style.display='inline'; Codehighlighter1_810_880_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">inq[v])&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_810_880_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_810_880_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inq[v]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;q.push(v);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /><img id="Codehighlighter1_920_2061_Open_Image" onclick="this.style.display='none'; Codehighlighter1_920_2061_Open_Text.style.display='none'; Codehighlighter1_920_2061_Closed_Image.style.display='inline'; Codehighlighter1_920_2061_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_920_2061_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_920_2061_Closed_Text.style.display='none'; Codehighlighter1_920_2061_Open_Image.style.display='inline'; Codehighlighter1_920_2061_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_920_2061_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_920_2061_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;cas;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">cas);<br /><img id="Codehighlighter1_979_2013_Open_Image" onclick="this.style.display='none'; Codehighlighter1_979_2013_Open_Text.style.display='none'; Codehighlighter1_979_2013_Closed_Image.style.display='inline'; Codehighlighter1_979_2013_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_979_2013_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_979_2013_Closed_Text.style.display='none'; Codehighlighter1_979_2013_Open_Image.style.display='inline'; Codehighlighter1_979_2013_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(cas</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_979_2013_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_979_2013_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sps&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(head,</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #0000ff">sizeof</span><span style="color: #000000">(head));<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n,</span><span style="color: #000000">&amp;</span><span style="color: #000000">m);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;a,b,c;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;tot&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;cnt&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img id="Codehighlighter1_1162_1259_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1162_1259_Open_Text.style.display='none'; Codehighlighter1_1162_1259_Closed_Image.style.display='inline'; Codehighlighter1_1162_1259_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1162_1259_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1162_1259_Closed_Text.style.display='none'; Codehighlighter1_1162_1259_Open_Image.style.display='inline'; Codehighlighter1_1162_1259_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;m;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1162_1259_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1162_1259_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d%d%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">a,</span><span style="color: #000000">&amp;</span><span style="color: #000000">b,</span><span style="color: #000000">&amp;</span><span style="color: #000000">c);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedge(a,b,c);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;addedge(b,a,c);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_1311_1373_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1311_1373_Open_Text.style.display='none'; Codehighlighter1_1311_1373_Closed_Image.style.display='inline'; Codehighlighter1_1311_1373_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1311_1373_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1311_1373_Closed_Text.style.display='none'; Codehighlighter1_1311_1373_Open_Image.style.display='inline'; Codehighlighter1_1311_1373_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1311_1373_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1311_1373_Open_Text"><span style="color: #000000">{&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">w[i]);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cnt&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;w[i];<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spfa();<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;flag&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img id="Codehighlighter1_1454_1564_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1454_1564_Open_Text.style.display='none'; Codehighlighter1_1454_1564_Closed_Image.style.display='inline'; Codehighlighter1_1454_1564_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1454_1564_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1454_1564_Closed_Text.style.display='none'; Codehighlighter1_1454_1564_Open_Image.style.display='inline'; Codehighlighter1_1454_1564_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1454_1564_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1454_1564_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tot&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;dist[i];<br /><img id="Codehighlighter1_1513_1555_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1513_1555_Open_Text.style.display='none'; Codehighlighter1_1513_1555_Closed_Image.style.display='inline'; Codehighlighter1_1513_1555_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1513_1555_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1513_1555_Closed_Text.style.display='none'; Codehighlighter1_1513_1555_Open_Image.style.display='inline'; Codehighlighter1_1513_1555_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(dist[i]&nbsp;</span><span style="color: #000000">==</span><span style="color: #000000">&nbsp;INF)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1513_1555_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1513_1555_Open_Text"><span style="color: #000000">{&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">"</span><span style="color: #000000">impossible</span><span style="color: #000000">"</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;flag&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;</span><span style="color: #0000ff">break</span><span style="color: #000000">;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(flag)&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">100001</span><span style="color: #000000">;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[i]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cnt&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;cnt</span><span style="color: #000000">/</span><span style="color: #000000">2</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_1737_1857_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1737_1857_Open_Text.style.display='none'; Codehighlighter1_1737_1857_Closed_Image.style.display='inline'; Codehighlighter1_1737_1857_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1737_1857_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1737_1857_Closed_Text.style.display='none'; Codehighlighter1_1737_1857_Open_Image.style.display='inline'; Codehighlighter1_1737_1857_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;n;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1737_1857_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1737_1857_Open_Text"><span style="color: #000000">{<br /><img id="Codehighlighter1_1785_1848_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1785_1848_Open_Text.style.display='none'; Codehighlighter1_1785_1848_Closed_Image.style.display='inline'; Codehighlighter1_1785_1848_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1785_1848_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1785_1848_Closed_Text.style.display='none'; Codehighlighter1_1785_1848_Open_Image.style.display='inline'; Codehighlighter1_1785_1848_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;j&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;tot;&nbsp;j&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">&nbsp;dist[i];&nbsp;j</span><span style="color: #000000">--</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1785_1848_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1785_1848_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f[j]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;max(f[j],f[j</span><span style="color: #000000">-</span><span style="color: #000000">dist[i]]&nbsp;</span><span style="color: #000000">+</span><span style="color: #000000">&nbsp;w[i]);<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img id="Codehighlighter1_1910_2007_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1910_2007_Open_Text.style.display='none'; Codehighlighter1_1910_2007_Closed_Image.style.display='inline'; Codehighlighter1_1910_2007_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1910_2007_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1910_2007_Closed_Text.style.display='none'; Codehighlighter1_1910_2007_Open_Image.style.display='inline'; Codehighlighter1_1910_2007_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;&nbsp;i&nbsp;</span><span style="color: #000000">&lt;=</span><span style="color: #000000">&nbsp;tot;&nbsp;i</span><span style="color: #000000">++</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1910_2007_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1910_2007_Open_Text"><span style="color: #000000">{<br /><img id="Codehighlighter1_1938_1998_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1938_1998_Open_Text.style.display='none'; Codehighlighter1_1938_1998_Closed_Image.style.display='inline'; Codehighlighter1_1938_1998_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1938_1998_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1938_1998_Closed_Text.style.display='none'; Codehighlighter1_1938_1998_Open_Image.style.display='inline'; Codehighlighter1_1938_1998_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(f[i]&nbsp;</span><span style="color: #000000">&gt;=</span><span style="color: #000000">&nbsp;cnt)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1938_1998_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1938_1998_Open_Text"><span style="color: #000000">{<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">i</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;&nbsp;system("pause");</span><span style="color: #008000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span></div><img src ="http://www.cppblog.com/cxysteven/aggbug/155349.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-08 13:21 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/08/155349.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MILLER-RABIN和大数因子分解</title><link>http://www.cppblog.com/cxysteven/archive/2011/09/02/154934.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Fri, 02 Sep 2011 03:12:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/09/02/154934.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/154934.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/09/02/154934.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/154934.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/154934.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><span style="color: #000000">#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdio</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstring</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cmath</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">cstdlib</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">algorithm</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">using</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">namespace</span><span style="color: #000000">&nbsp;std;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;maxn&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1100</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />typedef&nbsp;&nbsp;__int64&nbsp;bigint;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />bigint&nbsp;gcd(bigint&nbsp;A,bigint&nbsp;B)<br /><img id="Codehighlighter1_213_308_Open_Image" onclick="this.style.display='none'; Codehighlighter1_213_308_Open_Text.style.display='none'; Codehighlighter1_213_308_Closed_Image.style.display='inline'; Codehighlighter1_213_308_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_213_308_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_213_308_Closed_Text.style.display='none'; Codehighlighter1_213_308_Open_Image.style.display='inline'; Codehighlighter1_213_308_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_213_308_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_213_308_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(B)<br /><img id="Codehighlighter1_232_292_Open_Image" onclick="this.style.display='none'; Codehighlighter1_232_292_Open_Text.style.display='none'; Codehighlighter1_232_292_Closed_Image.style.display='inline'; Codehighlighter1_232_292_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_232_292_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_232_292_Closed_Text.style.display='none'; Codehighlighter1_232_292_Open_Image.style.display='inline'; Codehighlighter1_232_292_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_232_292_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_232_292_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bigint&nbsp;C&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;A</span><span style="color: #000000">%</span><span style="color: #000000">B;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;B;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;C;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;A;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />bigint&nbsp;product_mod(bigint&nbsp;A,bigint&nbsp;B,bigint&nbsp;C)<br /><img id="Codehighlighter1_357_568_Open_Image" onclick="this.style.display='none'; Codehighlighter1_357_568_Open_Text.style.display='none'; Codehighlighter1_357_568_Closed_Image.style.display='inline'; Codehighlighter1_357_568_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_357_568_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_357_568_Closed_Text.style.display='none'; Codehighlighter1_357_568_Open_Image.style.display='inline'; Codehighlighter1_357_568_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_357_568_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_357_568_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;bigint&nbsp;R&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,D&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;A;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(B)<br /><img id="Codehighlighter1_400_552_Open_Image" onclick="this.style.display='none'; Codehighlighter1_400_552_Open_Text.style.display='none'; Codehighlighter1_400_552_Closed_Image.style.display='inline'; Codehighlighter1_400_552_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_400_552_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_400_552_Closed_Text.style.display='none'; Codehighlighter1_400_552_Open_Image.style.display='inline'; Codehighlighter1_400_552_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_400_552_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_400_552_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(B</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">)<br /><img id="Codehighlighter1_426_488_Open_Image" onclick="this.style.display='none'; Codehighlighter1_426_488_Open_Text.style.display='none'; Codehighlighter1_426_488_Closed_Image.style.display='inline'; Codehighlighter1_426_488_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_426_488_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_426_488_Closed_Text.style.display='none'; Codehighlighter1_426_488_Open_Image.style.display='inline'; Codehighlighter1_426_488_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_426_488_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_426_488_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;R&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(R</span><span style="color: #000000">+</span><span style="color: #000000">D);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(R</span><span style="color: #000000">&gt;</span><span style="color: #000000">C)&nbsp;&nbsp;R&nbsp;</span><span style="color: #000000">-=</span><span style="color: #000000">&nbsp;C;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D&nbsp;</span><span style="color: #000000">+=</span><span style="color: #000000">&nbsp;D;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(D</span><span style="color: #000000">&gt;</span><span style="color: #000000">C)&nbsp;&nbsp;D&nbsp;</span><span style="color: #000000">-=</span><span style="color: #000000">&nbsp;C;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B&nbsp;</span><span style="color: #000000">&gt;&gt;=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;R;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />bigint&nbsp;power_mod(bigint&nbsp;A,bigint&nbsp;B,bigint&nbsp;C)<br /><img id="Codehighlighter1_615_770_Open_Image" onclick="this.style.display='none'; Codehighlighter1_615_770_Open_Text.style.display='none'; Codehighlighter1_615_770_Closed_Image.style.display='inline'; Codehighlighter1_615_770_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_615_770_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_615_770_Closed_Text.style.display='none'; Codehighlighter1_615_770_Open_Image.style.display='inline'; Codehighlighter1_615_770_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_615_770_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_615_770_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;bigint&nbsp;R&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">,D&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;A;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(B)<br /><img id="Codehighlighter1_658_754_Open_Image" onclick="this.style.display='none'; Codehighlighter1_658_754_Open_Text.style.display='none'; Codehighlighter1_658_754_Closed_Image.style.display='inline'; Codehighlighter1_658_754_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_658_754_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_658_754_Closed_Text.style.display='none'; Codehighlighter1_658_754_Open_Image.style.display='inline'; Codehighlighter1_658_754_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_658_754_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_658_754_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(B</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;&nbsp;R&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;product_mod(R,D,C);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;product_mod(D,D,C);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B&nbsp;</span><span style="color: #000000">&gt;&gt;=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;R;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img id="Codehighlighter1_784_810_Open_Image" onclick="this.style.display='none'; Codehighlighter1_784_810_Open_Text.style.display='none'; Codehighlighter1_784_810_Closed_Image.style.display='inline'; Codehighlighter1_784_810_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_784_810_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_784_810_Closed_Text.style.display='none'; Codehighlighter1_784_810_Open_Image.style.display='inline'; Codehighlighter1_784_810_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;pri[]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_784_810_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_784_810_Open_Text"><span style="color: #000000">{</span><span style="color: #000000">2</span><span style="color: #000000">,</span><span style="color: #000000">3</span><span style="color: #000000">,</span><span style="color: #000000">5</span><span style="color: #000000">,</span><span style="color: #000000">7</span><span style="color: #000000">,</span><span style="color: #000000">11</span><span style="color: #000000">,</span><span style="color: #000000">13</span><span style="color: #000000">,</span><span style="color: #000000">17</span><span style="color: #000000">,</span><span style="color: #000000">19</span><span style="color: #000000">,</span><span style="color: #000000">23</span><span style="color: #000000">,</span><span style="color: #000000">29</span><span style="color: #000000">}</span></span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">bool</span><span style="color: #000000">&nbsp;Miller_Rabin(bigint&nbsp;n)<br /><img id="Codehighlighter1_841_1295_Open_Image" onclick="this.style.display='none'; Codehighlighter1_841_1295_Open_Text.style.display='none'; Codehighlighter1_841_1295_Closed_Image.style.display='inline'; Codehighlighter1_841_1295_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_841_1295_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_841_1295_Closed_Text.style.display='none'; Codehighlighter1_841_1295_Open_Image.style.display='inline'; Codehighlighter1_841_1295_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_841_1295_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_841_1295_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(n</span><span style="color: #000000">&lt;</span><span style="color: #000000">2</span><span style="color: #000000">)&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(n</span><span style="color: #000000">==</span><span style="color: #000000">2</span><span style="color: #000000">)&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">(n</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">))&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;bigint&nbsp;k&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">,i,j,m,a;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #000000">!</span><span style="color: #000000">(m</span><span style="color: #000000">&amp;</span><span style="color: #000000">1</span><span style="color: #000000">))&nbsp;&nbsp;m&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;(m</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">1</span><span style="color: #000000">),k</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">10</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_1027_1276_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1027_1276_Open_Text.style.display='none'; Codehighlighter1_1027_1276_Closed_Image.style.display='inline'; Codehighlighter1_1027_1276_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1027_1276_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1027_1276_Closed_Text.style.display='none'; Codehighlighter1_1027_1276_Open_Image.style.display='inline'; Codehighlighter1_1027_1276_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1027_1276_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1027_1276_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(pri[i]</span><span style="color: #000000">&gt;=</span><span style="color: #000000">n)&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;power_mod(pri[i],m,n);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(a</span><span style="color: #000000">==</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;&nbsp;</span><span style="color: #0000ff">continue</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(j</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;j</span><span style="color: #000000">&lt;</span><span style="color: #000000">k;j</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_1161_1238_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1161_1238_Open_Text.style.display='none'; Codehighlighter1_1161_1238_Closed_Image.style.display='inline'; Codehighlighter1_1161_1238_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1161_1238_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1161_1238_Closed_Text.style.display='none'; Codehighlighter1_1161_1238_Open_Image.style.display='inline'; Codehighlighter1_1161_1238_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1161_1238_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1161_1238_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(a</span><span style="color: #000000">==</span><span style="color: #000000">n</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;product_mod(a,a,n);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(j</span><span style="color: #000000">==</span><span style="color: #000000">k)&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">false</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">true</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />bigint&nbsp;pollard_rho(bigint&nbsp;C,bigint&nbsp;N)<br /><img id="Codehighlighter1_1335_1577_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1335_1577_Open_Text.style.display='none'; Codehighlighter1_1335_1577_Closed_Image.style.display='inline'; Codehighlighter1_1335_1577_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1335_1577_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1335_1577_Closed_Text.style.display='none'; Codehighlighter1_1335_1577_Open_Image.style.display='inline'; Codehighlighter1_1335_1577_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1335_1577_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1335_1577_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;bigint&nbsp;I,X,Y,K,D;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;I&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;X</span><span style="color: #000000">=</span><span style="color: #000000">Y</span><span style="color: #000000">=</span><span style="color: #000000">rand()</span><span style="color: #000000">%</span><span style="color: #000000">N;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;K&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">2</span><span style="color: #000000">;<br /><img id="Codehighlighter1_1405_1549_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1405_1549_Open_Text.style.display='none'; Codehighlighter1_1405_1549_Closed_Image.style.display='inline'; Codehighlighter1_1405_1549_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1405_1549_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1405_1549_Closed_Text.style.display='none'; Codehighlighter1_1405_1549_Open_Image.style.display='inline'; Codehighlighter1_1405_1549_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">do</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1405_1549_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1405_1549_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I</span><span style="color: #000000">++</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;gcd(N</span><span style="color: #000000">+</span><span style="color: #000000">Y</span><span style="color: #000000">-</span><span style="color: #000000">X,N);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(D</span><span style="color: #000000">&gt;</span><span style="color: #000000">1</span><span style="color: #000000">&amp;&amp;</span><span style="color: #000000">D</span><span style="color: #000000">&lt;</span><span style="color: #000000">N)&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;D;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(I</span><span style="color: #000000">==</span><span style="color: #000000">K)&nbsp;&nbsp;Y</span><span style="color: #000000">=</span><span style="color: #000000">X,K</span><span style="color: #000000">&lt;&lt;=</span><span style="color: #000000">1</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X</span><span style="color: #000000">=</span><span style="color: #000000">(product_mod(X,X,N)</span><span style="color: #000000">+</span><span style="color: #000000">N</span><span style="color: #000000">-</span><span style="color: #000000">C)</span><span style="color: #000000">%</span><span style="color: #000000">N;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #0000ff">while</span><span style="color: #000000">(Y</span><span style="color: #000000">!=</span><span style="color: #000000">X);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;N;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" />bigint&nbsp;allFactor[maxn];<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;fn;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;rho(bigint&nbsp;N)<br /><img id="Codehighlighter1_1630_1807_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1630_1807_Open_Text.style.display='none'; Codehighlighter1_1630_1807_Closed_Image.style.display='inline'; Codehighlighter1_1630_1807_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1630_1807_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1630_1807_Closed_Text.style.display='none'; Codehighlighter1_1630_1807_Open_Image.style.display='inline'; Codehighlighter1_1630_1807_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1630_1807_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1630_1807_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(Miller_Rabin(N))<br /><img id="Codehighlighter1_1660_1710_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1660_1710_Open_Text.style.display='none'; Codehighlighter1_1660_1710_Closed_Image.style.display='inline'; Codehighlighter1_1660_1710_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1660_1710_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1660_1710_Closed_Text.style.display='none'; Codehighlighter1_1660_1710_Open_Image.style.display='inline'; Codehighlighter1_1660_1710_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1660_1710_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1660_1710_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;allFactor[</span><span style="color: #000000">++</span><span style="color: #000000">fn]</span><span style="color: #000000">=</span><span style="color: #000000">N;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;bigint&nbsp;T&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;N;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(T</span><span style="color: #000000">&gt;=</span><span style="color: #000000">N)&nbsp;T&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;pollard_rho(rand()</span><span style="color: #000000">%</span><span style="color: #000000">(N</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,N);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;rho(T);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;rho(N</span><span style="color: #000000">/</span><span style="color: #000000">T);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main()<br /><img id="Codehighlighter1_1820_2239_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1820_2239_Open_Text.style.display='none'; Codehighlighter1_1820_2239_Closed_Image.style.display='inline'; Codehighlighter1_1820_2239_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1820_2239_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1820_2239_Closed_Text.style.display='none'; Codehighlighter1_1820_2239_Open_Image.style.display='inline'; Codehighlighter1_1820_2239_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1820_2239_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1820_2239_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;bigint&nbsp;n;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">while</span><span style="color: #000000">(</span><span style="color: #0000ff">true</span><span style="color: #000000">)<br /><img id="Codehighlighter1_1856_2223_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1856_2223_Open_Text.style.display='none'; Codehighlighter1_1856_2223_Closed_Image.style.display='inline'; Codehighlighter1_1856_2223_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1856_2223_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1856_2223_Closed_Text.style.display='none'; Codehighlighter1_1856_2223_Open_Image.style.display='inline'; Codehighlighter1_1856_2223_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1856_2223_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1856_2223_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanf(</span><span style="color: #000000">"</span><span style="color: #000000">%I64d</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">&amp;</span><span style="color: #000000">n);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(n</span><span style="color: #000000">&lt;=</span><span style="color: #000000">0</span><span style="color: #000000">)&nbsp;&nbsp;</span><span style="color: #0000ff">break</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(Miller_Rabin(n))&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;%I64d\n</span><span style="color: #000000">"</span><span style="color: #000000">,n);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">else</span><span style="color: #000000"><br /><img id="Codehighlighter1_1987_2197_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1987_2197_Open_Text.style.display='none'; Codehighlighter1_1987_2197_Closed_Image.style.display='inline'; Codehighlighter1_1987_2197_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1987_2197_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1987_2197_Closed_Text.style.display='none'; Codehighlighter1_1987_2197_Open_Image.style.display='inline'; Codehighlighter1_1987_2197_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1987_2197_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_1987_2197_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rho(n);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(allFactor</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">,allFactor</span><span style="color: #000000">+</span><span style="color: #000000">fn</span><span style="color: #000000">+</span><span style="color: #000000">1</span><span style="color: #000000">);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;i</span><span style="color: #000000">=</span><span style="color: #000000">1</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;=</span><span style="color: #000000">fn;i</span><span style="color: #000000">++</span><span style="color: #000000">)<br /><img id="Codehighlighter1_2120_2187_Open_Image" onclick="this.style.display='none'; Codehighlighter1_2120_2187_Open_Text.style.display='none'; Codehighlighter1_2120_2187_Closed_Image.style.display='inline'; Codehighlighter1_2120_2187_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_2120_2187_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_2120_2187_Closed_Text.style.display='none'; Codehighlighter1_2120_2187_Open_Image.style.display='inline'; Codehighlighter1_2120_2187_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_2120_2187_Closed_Text"><img src="http://www.cppblog.com/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_2120_2187_Open_Text"><span style="color: #000000">{<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;%I64d\n</span><span style="color: #000000">"</span><span style="color: #000000">,allFactor[i]);<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">endl;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: #000000"><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /><br /><img align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif"  alt="" /></span></div><img src ="http://www.cppblog.com/cxysteven/aggbug/154934.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-09-02 11:12 <a href="http://www.cppblog.com/cxysteven/archive/2011/09/02/154934.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MANACHER'S ALGORITHM</title><link>http://www.cppblog.com/cxysteven/archive/2011/08/31/154792.html</link><dc:creator>成幸毅</dc:creator><author>成幸毅</author><pubDate>Wed, 31 Aug 2011 08:02:00 GMT</pubDate><guid>http://www.cppblog.com/cxysteven/archive/2011/08/31/154792.html</guid><wfw:comment>http://www.cppblog.com/cxysteven/comments/154792.html</wfw:comment><comments>http://www.cppblog.com/cxysteven/archive/2011/08/31/154792.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/cxysteven/comments/commentRss/154792.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/cxysteven/services/trackbacks/154792.html</trackback:ping><description><![CDATA[<div>【转】<a href="http://blog.csdn.net/ggggiqnypgjg/article/details/6645824">http://blog.csdn.net/ggggiqnypgjg/article/details/6645824</a><br /><br />
<div align="center"><span style="font-size: 24px"><strong><span style="font-family: courier new">O</span><span style="font-family: 宋体">（</span><span style="font-family: courier new">n</span><span style="font-family: 宋体">）回文子串算法</span></strong></span><span style="font-family: courier new"></span></div>
<p>注：转载的这篇文章，我发现下面那个<span style="color: #ff0000">源代码有点bug</span>。。。在下一篇博客中改正了。。</p>
<p><span style="font-size: 16px"><span style="font-family: 宋体"></span></span>&nbsp;</p>
<p><span style="font-size: 16px"><span style="font-family: 宋体">&nbsp;&nbsp;&nbsp; 这里，我介绍一下</span><span style="font-family: courier new">O</span><span style="font-family: 宋体">（</span><span style="font-family: courier new">n</span><span style="font-family: 宋体">）回文串处理的一种方法。</span><span style="font-family: courier new">Manacher</span><span style="font-family: 宋体">算法</span><span style="font-family: courier new">.</span><br /><span style="font-family: courier new"></span></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: 宋体">原文地址：</span></span><span style="font-size: 16px"><span style="font-family: 宋体"><br /></span></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: courier new"><a id="url_1" href="http://zhuhongcheng.wordpress.com/2009/08/02/a-simple-linear-time-algorithm-for-finding-longest-palindrome-sub-string/" target="_blank">http://zhuhongcheng.wordpress.com/2009/08/02/a-simple-linear-time-algorithm-for-finding-longest-palindrome-sub-string/</a></span><br /></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: 宋体">&nbsp; &nbsp; 其实原文说得是比较清楚的，只是英文的，我这里写一份中文的吧。</span><span style="font-family: courier new"></span><br /></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: 宋体">&nbsp; &nbsp; 首先：大家都知道什么叫回文串吧，这个算法要解决的就是一个字符串中最长的回文子串有多长。这个算法可以在</span><span style="font-family: courier new">O</span><span style="font-family: 宋体">（</span><span style="font-family: courier new">n</span><span style="font-family: 宋体">）的时间复杂度内既线性时间复杂度的情况下，求出以每个字符为中心的最长回文有多长，</span><span style="font-family: courier new"></span><br /></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: 宋体">&nbsp; &nbsp; 这个算法有一个很巧妙的地方，它把奇数的回文串和偶数的回文串统一起来考虑了。这一点一直是在做回文串问题中时比较烦的地方。这个算法还有一个很好的地方就是充分利用了字符匹配的特殊性，避免了大量不必要的重复匹配。</span><span style="font-family: courier new"></span><br /></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: 宋体">&nbsp; &nbsp; 算法大致过程是这样。先在每两个相邻字符中间插入一个分隔符，当然这个分隔符要在原串中没有出现过。一般可以用</span><span style="font-family: courier new">&#8216;#&#8217;</span><span style="font-family: 宋体">分隔。这样就非常巧妙的将奇数长度回文串与偶数长度回文串统一起来考虑了（见下面的一个例子，回文串长度全为奇数了），然后用一个辅助数组</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">记录以每个字符为中心的最长回文串的信息。</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">［</span><span style="font-family: courier new">id</span><span style="font-family: 宋体">］记录的是以字符</span><span style="font-family: courier new">str</span><span style="font-family: 宋体">［</span><span style="font-family: courier new">id</span><span style="font-family: 宋体">］为中心的最长回文串，当以</span><span style="font-family: courier new">str</span><span style="font-family: 宋体">［</span><span style="font-family: courier new">id</span><span style="font-family: 宋体">］为第一个字符，这个最长回文串向右延伸了</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">［</span><span style="font-family: courier new">id</span><span style="font-family: 宋体">］个字符。</span><span style="font-family: courier new"></span><br /></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: 宋体">&nbsp; &nbsp; 原串：</span><span style="font-family: courier new">&nbsp;&nbsp;&nbsp;&nbsp;w aa bwsw f d</span><br /></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: 宋体">&nbsp; &nbsp; 新串：</span><span style="font-family: courier new">&nbsp;&nbsp; # w<u># a # a #</u> b<u># w # s # w #</u> f # d #</span><br /></span><span style="font-size: 16px"><span style="font-family: 宋体">辅助数组</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">： &nbsp;</span><span style="font-family: courier new">1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1</span><br /><span style="font-family: 宋体">&nbsp; &nbsp; 这里有一个很好的性质，</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">［</span><span style="font-family: courier new">id</span><span style="font-family: 宋体">］</span><span style="font-family: courier new">-1</span><span style="font-family: 宋体">就是该回文子串在原串中的长度（包括&#8216;</span><span style="font-family: courier new">#</span><span style="font-family: 宋体">&#8217;）。如果这里不是特别清楚，可以自己拿出纸来画一画，自己体会体会。当然这里可能每个人写法不尽相同，不过我想大致思路应该是一样的吧。</span><span style="font-family: courier new"></span><br /><span style="font-family: 宋体">&nbsp; &nbsp; 好，我们继续。现在的关键问题就在于怎么在</span><span style="font-family: courier new">O</span><span style="font-family: 宋体">（</span><span style="font-family: courier new">n</span><span style="font-family: 宋体">）时间复杂度内求出</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">数组了。只要把这个</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">数组求出来，最长回文子串就可以直接扫一遍得出来了。</span><span style="font-family: courier new"></span><br /><span style="font-family: 宋体">&nbsp; &nbsp; 由于这个算法是线性从前往后扫的。那么当我们准备求</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">［</span><span style="font-family: courier new">i</span><span style="font-family: 宋体">］的时候，</span><span style="font-family: courier new">i</span><span style="font-family: 宋体">以前的</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">［</span><span style="font-family: courier new">j</span><span style="font-family: 宋体">］我们是已经得到了的。我们用</span><span style="font-family: courier new">mx</span><span style="font-family: 宋体">记在</span><span style="font-family: courier new">i</span><span style="font-family: 宋体">之前的回文串中，延伸至最右端的位置。同时用</span><span style="font-family: courier new">id</span><span style="font-family: 宋体">这个变量记下取得这个最优</span><span style="font-family: courier new">mx</span><span style="font-family: 宋体">时的</span><span style="font-family: courier new">id</span><span style="font-family: 宋体">值。（注：为了防止字符比较的时候越界，我在这个加了&#8216;</span><span style="font-family: courier new">#</span><span style="font-family: 宋体">&#8217;的字符串之前还加了另一个特殊字符&#8216;</span><span style="font-family: courier new">$</span><span style="font-family: 宋体">&#8217;，故我的新串下标是从</span><span style="font-family: courier new">1</span><span style="font-family: 宋体">开始的）</span><span style="font-family: courier new"></span><br /></span><span style="font-family: 宋体"><span style="font-size: 16px">好，到这里，我们可以先贴一份代码了。</span></span><span style="font-family: courier new"></span><br /></p><span style="font-family: courier new"></span>
<table cellspacing="0" cellpadding="0" width="80%">
<tbody>
<tr>
<td>
<div class="c"></div><span class="f10 s8"><a href="http://acm.uestc.edu.cn/bbs/simple/?t3258.html">复制代码</a></span> 
<div class="blockquote2" id="code1">
<ol><li><br />void pk()<br />{<br />&nbsp; &nbsp; int i;<br />&nbsp; &nbsp; int mx = 0;<br />&nbsp; &nbsp; int id;<br />&nbsp; &nbsp; for(i=1; i&lt;n; i++)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; if( mx &gt; i )<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;p[i] = MIN( p[2*id-i], mx-i );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;else<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;p[i] = 1;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for(; str[i+p[i]] == str[i-p[i]]; p[i]++)<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if( p[i] + i &gt; mx )<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;{<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;mx = p[i] + i;<br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;id = i;<br />&nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; }<br />}<br /></li></ol></div></td></tr></tbody></table><br />
<div align="left"><span style="font-size: 16px"><span style="font-family: courier new"><span style="color: red"></span></span><span style="font-family: 宋体">&nbsp; &nbsp;<span style="font-family: 宋体"></span>代码是不是很短啊，而且相当好写。很方便吧，还记得我上面说的这个算法避免了很多不必要的重复匹配吧。这是什么意思呢，其实这就是一句代码。</span></span><span style="font-family: courier new"></span></div>
<div align="left"><span style="font-family: 宋体"><br /></span></div>
<div align="left"><span style="font-family: courier new"><span style="color: black"></span></span><strong><span style="font-family: courier new"><span style="color: blue">if</span></span></strong><span style="font-family: courier new"><span style="color: #8b0000">(</span></span><span style="font-family: courier new"><span style="color: black"> mx </span></span><span style="font-family: courier new"><span style="color: #8b0000">&gt;</span></span><span style="font-family: courier new"><span style="color: black"> i</span></span><span style="font-family: courier new"><span style="color: #8b0000">)</span></span><span style="font-family: courier new"><span style="color: black"></span></span></div>
<div align="left"><span style="font-family: courier new"><span style="color: black">&nbsp; &nbsp; p</span></span><span style="font-family: courier new"><span style="color: #8b0000">[</span></span><span style="font-family: courier new"><span style="color: black">i</span></span><span style="font-family: courier new"><span style="color: #8b0000">]</span></span><span style="font-family: courier new"><span style="color: black"></span></span><span style="font-family: courier new"><span style="color: #8b0000">=</span></span><span style="font-family: courier new"><span style="color: black"><strong>MIN</strong></span></span><span style="font-family: courier new"><span style="color: #8b0000">(</span></span><span style="font-family: courier new"><span style="color: black"> p</span></span><span style="font-family: courier new"><span style="color: #8b0000">[</span></span><span style="font-family: courier new"><span style="color: purple">2</span></span><span style="font-family: courier new"><span style="color: #8b0000">*</span></span><span style="font-family: courier new"><span style="color: black">id</span></span><span style="font-family: courier new"><span style="color: #8b0000">-</span></span><span style="font-family: courier new"><span style="color: black">i</span></span><span style="font-family: courier new"><span style="color: #8b0000">],</span></span><span style="font-family: courier new"><span style="color: black"> mx</span></span><span style="font-family: courier new"><span style="color: #8b0000">-</span></span><span style="font-family: courier new"><span style="color: black">i</span></span><span style="font-family: courier new"><span style="color: #8b0000">);</span></span><span style="font-family: courier new"><span style="color: black"></span></span></div>
<div align="left"><span style="font-family: courier new"><span style="color: #8b0000"><br /></span></span></div>
<div align="left"><span style="font-family: 宋体"></span><span style="font-family: 宋体"></span><span style="font-size: 16px"><span style="font-family: 宋体">就是当前面比较的最远长度</span><span style="font-family: courier new">mx&gt;i</span><span style="font-family: 宋体">的时候，</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">［</span><span style="font-family: courier new">i</span><span style="font-family: 宋体">］有一个最小值。这个算法的核心思想就在这里，为什么</span><span style="font-family: courier new">P</span><span style="font-family: 宋体">数组满足这样一个性质呢</span><span style="font-family: courier new">?</span></span></div>
<div align="left"><span style="font-size: 16px"><span style="font-family: courier new">&nbsp; &nbsp;（下面的部分为图片形式）<br /><br /><br /></span></span></div>
<div align="left"><span style="font-size: 16px"><span style="font-family: courier new"><img alt="" src="http://hi.csdn.net/attachment/201107/29/0_1311958486Bbay.gif" /><br /><br /></span></span></div>
<div align="left"><span style="font-size: 16px"></span></div>
<div align="left"><span style="font-size: 16px"><span style="font-family: 宋体">&nbsp; &nbsp; 看完这个算法，你有可能会觉得这种算法在哪会用到呢？其实回文串后缀数组也可以做。只是复杂度是</span><span style="font-family: courier new">O</span><span style="font-family: 宋体">（</span><span style="font-family: courier new">n log n</span><span style="font-family: 宋体">）的，而且一般情况下也不会刻意去卡一个</span><span style="font-family: courier new">log n</span><span style="font-family: 宋体">的算法。可正好</span><span style="font-family: courier new">hdu</span><span style="font-family: 宋体">就有这么一题，你用后缀数组写怎么都得</span><span style="font-family: courier new">T</span><span style="font-family: 宋体">（当然应该是我写得太烂了）。不信的话大家也可以去试试这题。</span></span></div><span style="font-size: 16px">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a id="url_2" href="http://acm.hdu.edu.cn/showproblem.php?pid=3068" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=3068</a><br /><span style="font-family: 宋体">&nbsp; &nbsp; 另外，顺便附一份</span>AC<span style="font-family: 宋体">代码。</span><span style="font-family: courier new"></span><br />&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<a id="url_3" href="http://acm.hust.edu.cn:8080/judge/problem/viewSource.action?id=140283" target="_blank">http://acm.hust.edu.cn:8080/judge/problem/viewSource.action?id=140283</a><br /></span></div><img src ="http://www.cppblog.com/cxysteven/aggbug/154792.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/cxysteven/" target="_blank">成幸毅</a> 2011-08-31 16:02 <a href="http://www.cppblog.com/cxysteven/archive/2011/08/31/154792.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>