﻿<?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++博客-Brian Warehouse-随笔分类-SGU</title><link>http://www.cppblog.com/Icho/category/14622.html</link><description>Some birds aren`t meant to be caged, their feathers are just too bright... ...</description><language>zh-cn</language><lastBuildDate>Thu, 19 Aug 2010 17:06:07 GMT</lastBuildDate><pubDate>Thu, 19 Aug 2010 17:06:07 GMT</pubDate><ttl>60</ttl><item><title>SGU 118 Digital Root</title><link>http://www.cppblog.com/Icho/archive/2010/08/19/123977.html</link><dc:creator>Brian</dc:creator><author>Brian</author><pubDate>Thu, 19 Aug 2010 07:35:00 GMT</pubDate><guid>http://www.cppblog.com/Icho/archive/2010/08/19/123977.html</guid><wfw:comment>http://www.cppblog.com/Icho/comments/123977.html</wfw:comment><comments>http://www.cppblog.com/Icho/archive/2010/08/19/123977.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Icho/comments/commentRss/123977.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Icho/services/trackbacks/123977.html</trackback:ping><description><![CDATA[f(n) 是正整数n 的各位数字之和。如果 f(n) 是1位数那么n的数根就是f(n)，否则f(n) 的数根就是n 的数根。举例说明：987的数根是6 (9+8+7=24 2+4=6)。你的任务是找出这样表达式的数根: A1*A2*&#8230;*AN + A1*A2*&#8230;*AN-1 + &#8230; + A1*A2 + A1。
<p>输入包含K个测试样例。输入第一行会给出 K (1&lt;=K&lt;=5)。每个测试样例占一行。这一行的第一个数是一个正整数N (N&lt;=1000)。 接着是N个非负整数 (序列 A)。 均不超过10<sup>9</sup>。</p>
数根(百度知道): <a href="http://zhidao.baidu.com/question/29789035.html">http://zhidao.baidu.com/question/29789035.html</a><br>对于求余数的问题是我一开始就忽略了的地方，后来看了HPF的博客，才知道有这样的技巧:<br>&nbsp;&nbsp; (A+B)mod C = (A mod C) + (B mod C)<br>&nbsp;&nbsp; (AB) mod C = (A mod C) &#215; (B mod C)<br><br>C编译器 , 0MS&nbsp; 0KB , 如果你把前两句定义变量的语句对调，将会耗费 20MS 的时间<br>
<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: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()&nbsp;<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,j,K,N,temp,m;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">long</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;A;<br>&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">K);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">K;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;{<br>&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">N);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;&nbsp;j</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">N;&nbsp;j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;{<br>&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">temp);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp</span><span style="COLOR: #000000">%=</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(m</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">temp)</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(A</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">m)</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&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">,(A</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&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>}</span></div>
<img src ="http://www.cppblog.com/Icho/aggbug/123977.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Icho/" target="_blank">Brian</a> 2010-08-19 15:35 <a href="http://www.cppblog.com/Icho/archive/2010/08/19/123977.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SGU 111 Very simple problem</title><link>http://www.cppblog.com/Icho/archive/2010/08/19/123969.html</link><dc:creator>Brian</dc:creator><author>Brian</author><pubDate>Thu, 19 Aug 2010 06:36:00 GMT</pubDate><guid>http://www.cppblog.com/Icho/archive/2010/08/19/123969.html</guid><wfw:comment>http://www.cppblog.com/Icho/comments/123969.html</wfw:comment><comments>http://www.cppblog.com/Icho/archive/2010/08/19/123969.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Icho/comments/commentRss/123969.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Icho/services/trackbacks/123969.html</trackback:ping><description><![CDATA[下面的代码没有AC，没有AC的原因是 PE on test 8, 注意，是第八组。我难以理解，<span style="BACKGROUND-COLOR: yellow; COLOR: red">希望大牛们指教</span>！<br><br>
<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: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><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>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></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><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;__int64&nbsp;x,left,right,mid;<br>&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">x);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">x</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;((left</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">right)&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;二分查找</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mid</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(left</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">right)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(mid</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">mid</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">x)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;right</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">mid;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%I64d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,left);<br>&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>}</span></div>
<img src ="http://www.cppblog.com/Icho/aggbug/123969.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Icho/" target="_blank">Brian</a> 2010-08-19 14:36 <a href="http://www.cppblog.com/Icho/archive/2010/08/19/123969.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SGU 107 987654321 problem</title><link>http://www.cppblog.com/Icho/archive/2010/08/17/123698.html</link><dc:creator>Brian</dc:creator><author>Brian</author><pubDate>Tue, 17 Aug 2010 05:27:00 GMT</pubDate><guid>http://www.cppblog.com/Icho/archive/2010/08/17/123698.html</guid><wfw:comment>http://www.cppblog.com/Icho/comments/123698.html</wfw:comment><comments>http://www.cppblog.com/Icho/archive/2010/08/17/123698.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Icho/comments/commentRss/123698.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Icho/services/trackbacks/123698.html</trackback:ping><description><![CDATA[<p style="TEXT-INDENT: 2em">For given number N you must output amount of N-digit numbers, such, that <font color=#0000ff><strong>last digits of their square</strong></font> is equal to 987654321. </p>
<p style="TEXT-INDENT: 2em">Input</p>
<p style="TEXT-INDENT: 2em">Input contains integer number N (1&lt;=N&lt;=106) <br>首先想到的是枚举，我确实也这么做了，5~8无果，对于9，我的05年方正 Pentium M&nbsp; 上跑了足足1分半钟之久，我一开始还以为死循环了，后来出来八个结果，也去网上核对了一下，确实满足的只有这八个:<br>&nbsp;&nbsp; 111111111<br>&nbsp;&nbsp; 119357639<br>&nbsp;&nbsp; 380642361<br>&nbsp;&nbsp; 388888889<br>&nbsp;&nbsp; 611111111<br>&nbsp;&nbsp; 619357639<br>&nbsp;&nbsp; 880642361<br>&nbsp;&nbsp; 888888889<br>我没有学过数论，找到一篇博文，对于数论解法讲的还算清楚，不过用pascal写就，我自己用C写了一下，第三次AC了。博文链接如下：<a href="http://blog.csdn.net/Skyprophet/archive/2009/10/05/4634801.aspx"><font color=#282e0e>http://blog.csdn.net/Skyprophet/archive/2009/10/05/4634801.aspx</font></a><br></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: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;N,i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br>&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">N);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(N</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">0\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(N</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">8\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">72</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">N</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&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>}</span></div>
336MS&nbsp; 0K
<img src ="http://www.cppblog.com/Icho/aggbug/123698.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Icho/" target="_blank">Brian</a> 2010-08-17 13:27 <a href="http://www.cppblog.com/Icho/archive/2010/08/17/123698.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SGU 105 Div 3</title><link>http://www.cppblog.com/Icho/archive/2010/08/17/123697.html</link><dc:creator>Brian</dc:creator><author>Brian</author><pubDate>Tue, 17 Aug 2010 05:26:00 GMT</pubDate><guid>http://www.cppblog.com/Icho/archive/2010/08/17/123697.html</guid><wfw:comment>http://www.cppblog.com/Icho/comments/123697.html</wfw:comment><comments>http://www.cppblog.com/Icho/archive/2010/08/17/123697.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Icho/comments/commentRss/123697.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Icho/services/trackbacks/123697.html</trackback:ping><description><![CDATA[<div class="bct fc05 fc11 nbw-blog ztag js-fs2">
<p align=justify>There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Given first N elements of that sequence. You must determine amount of numbers in it that are divisible by 3. (找出这个数列1-N号元素中能被3整除的有多少个)</p>
<p align=justify></p>
<strong>
<p align=justify>Input</p>
</strong>
<p align=justify>Input contains N (1&lt;=N&lt;=2<sup>31</sup> - 1). </p>
<p align=justify></p>
<strong>
<p align=justify>一个数乘以10以后，模3后结果不变，第2，5，8，11，14，17........项满足要求<br></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: 14pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;N;<br>&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">N);<br>&nbsp;&nbsp;&nbsp;&nbsp;N</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">;<br>&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">,&nbsp;N</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">(N</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000; FONT-SIZE: 14pt">));<br>&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>}&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;0K&nbsp;&nbsp;0MS</span></div>
</strong></div>
<img src ="http://www.cppblog.com/Icho/aggbug/123697.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Icho/" target="_blank">Brian</a> 2010-08-17 13:26 <a href="http://www.cppblog.com/Icho/archive/2010/08/17/123697.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SGU 102 Coprimes</title><link>http://www.cppblog.com/Icho/archive/2010/08/17/123696.html</link><dc:creator>Brian</dc:creator><author>Brian</author><pubDate>Tue, 17 Aug 2010 05:24:00 GMT</pubDate><guid>http://www.cppblog.com/Icho/archive/2010/08/17/123696.html</guid><wfw:comment>http://www.cppblog.com/Icho/comments/123696.html</wfw:comment><comments>http://www.cppblog.com/Icho/archive/2010/08/17/123696.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Icho/comments/commentRss/123696.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Icho/services/trackbacks/123696.html</trackback:ping><description><![CDATA[题目简单翻译：对于给出的整数 N (1&lt;=N&lt;=10<sup>4</sup>) ，找出所有不大于N且与N互质的正整数<font color=#3366ff><strong>个数</strong></font>。<br>两个正整数A和B互质的意思是它们的最大公约数是1。现看一下百度百科上关于<a href="http://baike.baidu.com/view/10626.htm?fr=ala0_1_1" target=_blank><font color=#282e0e>质数</font></a>的定义吧。<br><font color=#333333>当我采用直接数的办法AC后，我发现<font color=#3366ff>跟质数完全没有关系。</font></font>
<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: 14pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #000000">#include</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;gcd(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;m,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;greatest&nbsp;common&nbsp;divisor</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">m</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(r)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;辗转相除法，TAOCP的第一个算法</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">r;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">m</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">n;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;n;<br>}<br><br></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;N,c</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;must&nbsp;start&nbsp;with&nbsp;1&nbsp;!</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&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">N);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">N;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(gcd(N,i)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;question&nbsp;hint</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%d</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,c);<br>&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>}<br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;&nbsp;102&nbsp;&nbsp;.C&nbsp;Accepted&nbsp;0&nbsp;ms&nbsp;0&nbsp;kb&nbsp;</span></div>
<img src ="http://www.cppblog.com/Icho/aggbug/123696.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Icho/" target="_blank">Brian</a> 2010-08-17 13:24 <a href="http://www.cppblog.com/Icho/archive/2010/08/17/123696.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SGU 113 Nearly prime numbers</title><link>http://www.cppblog.com/Icho/archive/2010/08/17/123695.html</link><dc:creator>Brian</dc:creator><author>Brian</author><pubDate>Tue, 17 Aug 2010 05:23:00 GMT</pubDate><guid>http://www.cppblog.com/Icho/archive/2010/08/17/123695.html</guid><wfw:comment>http://www.cppblog.com/Icho/comments/123695.html</wfw:comment><comments>http://www.cppblog.com/Icho/archive/2010/08/17/123695.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Icho/comments/commentRss/123695.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Icho/services/trackbacks/123695.html</trackback:ping><description><![CDATA[<div class="bct fc05 fc11 nbw-blog ztag js-fs2">
<p align=justify>Nearly prime number is an integer positive number for which it is possible to find such primes <em>P<sub>1</sub></em></sub> and <em>P<sub>2</sub></em></sub> that given number is equal to <em>P<sub>1</sub>*P<sub>2</sub></em></sub>. There is given a sequence on <em>N</em> integer positive numbers, you are to write a program that prints &#8220;Yes&#8221; if given number is nearly prime and &#8220;No&#8221; otherwise. </p>
<p align=justify></p>
<strong>
<p align=justify>Input</p>
</strong>
<p align=justify>Input file consists of <em>N+</em>1 numbers. First is positive integer <em>N (1<font face=Symbol>&#163;</font>N<font face=Symbol>&#163;</font>10)</em>. Next <em>N</em> numbers followed by <em>N</em>. Each number is not greater than <em>10<sup>9</sup></em></sup>. All numbers separated by whitespace(s). </p>
<p align=justify></p>
<strong>
<p align=justify>Output</p>
</strong>
<p align=justify>Write a line in output file for each number of given sequence. Write &#8220;Yes&#8221; in it if given number is nearly prime and &#8220;No&#8221; in other case. <br>不用管Nearly prime numbers 到底是个什么数，总之是两个质数的乘积就对了。枚举的范围: 2 ~ 32000 (<em>10<sup>4.5 </sup>)<br></em>我的思路是: 首先把2~32000之间的所有素数都存放在一个数组里，然后当你输入一个数据时，先让其逐一模除这个数组里的素数，一旦模除结果为0，则计算出他们的商，再判断商是否也为素数。<strong><font color=#ff0000>注意数据是两个数的平方的处理<br></font></strong></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: 14pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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">stdio.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">math.h</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;prime[</span><span style="COLOR: #000000">30000</span><span style="COLOR: #000000">],M</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;isP(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">判断是否为素数,非常精确而高效</span><span style="COLOR: #008000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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"><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000; FONT-SIZE: 14pt">{<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">,t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sqrt(n);<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;((n&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">(n&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">))&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;(n&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">(n&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">))&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(n&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">(n&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">))&nbsp;</span><span style="COLOR: #000000">||</span><span style="COLOR: #000000">&nbsp;(n&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">(n&nbsp;</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">7</span><span style="COLOR: #000000">)))<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&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 title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(;&nbsp;i</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">t;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(n</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">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 title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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 title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;isNP(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n)<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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"><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000">{<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,t</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">sqrt(n),r;<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(;&nbsp;prime[i]</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">t;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;平方在此处理</span><span style="COLOR: #008000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="COLOR: #000000">&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"><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000">{<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(n</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">prime[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;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;模除试商</span><span style="COLOR: #008000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="COLOR: #000000">&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"><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000">{<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">n</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">prime[i];&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;求商</span><span style="COLOR: #008000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(isP(r))<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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 title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;main()<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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"><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000">{<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">,N,m;&nbsp;&nbsp;&nbsp;&nbsp;<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">&nbsp;(prime[M</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">32000</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(isP(i))<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prime[M</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">i;<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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">N);<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(N</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" 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"><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000">{<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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">m);<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(m</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">)<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Yes\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;<font color=#ff0000>程序中唯一未解决的问题,望各路大牛指教!</font></span><span style="COLOR: #008000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,isNP(m)&nbsp;</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Yes</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">:</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">No</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&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 title="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" alt="SGU 113 Nearly prime numbers  - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span><span style="COLOR: #000000"><br></span></div>
</div>
<img src ="http://www.cppblog.com/Icho/aggbug/123695.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Icho/" target="_blank">Brian</a> 2010-08-17 13:23 <a href="http://www.cppblog.com/Icho/archive/2010/08/17/123695.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SGU 112 a^b-b^a (Java Edition)</title><link>http://www.cppblog.com/Icho/archive/2010/08/17/123694.html</link><dc:creator>Brian</dc:creator><author>Brian</author><pubDate>Tue, 17 Aug 2010 05:21:00 GMT</pubDate><guid>http://www.cppblog.com/Icho/archive/2010/08/17/123694.html</guid><wfw:comment>http://www.cppblog.com/Icho/comments/123694.html</wfw:comment><comments>http://www.cppblog.com/Icho/archive/2010/08/17/123694.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Icho/comments/commentRss/123694.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Icho/services/trackbacks/123694.html</trackback:ping><description><![CDATA[<p align=justify>You are given natural numbers a and b. Find a<sup>b</sup>-b<sup>a</sup>. </p>
<p align=justify></p>
<strong>
<p align=justify>Input</p>
</strong>
<p align=justify>Input contains numbers a and b (1&#8804;a,b&#8804;100). </p>
<p align=justify></p>
<strong>
<p align=justify>Output</p>
</strong>
<p align=justify>Write answer to output. </p>
<p align=justify></p>
<p align=justify>Sample Input</p>
<font face="Courier New">
<pre>2 3
</pre>
<p align=justify></p>
</font>
<p align=justify>Sample Output</p>
<font face="Courier New">
<pre>-1</pre>
</font>
<p>一看到这种题目，就想到高精度算法，Java的大数。<br>此举确实流氓了一点，不过确实过了，鄙人的第一题SGU啊，不许打击。<br>过段时间看看能不能写出 <strong>(C++ Edition)</strong> ,啥也别说了，上代码。<br></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: 14pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">imp<wbr>ort</span><span style="COLOR: #000000">&nbsp;java.math.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">imp<wbr>ort</span><span style="COLOR: #000000">&nbsp;java.util.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;Solution<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" 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"><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000">{<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&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"><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" src="http://www.cppblog.com/Images/dot.gif"></span><span><span style="COLOR: #000000">{<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scanner&nbsp;in&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Scanner(System.in);<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" 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;a&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;in.nextInt();<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" 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;b&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;in.nextInt();<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BigInteger&nbsp;A</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">BigInteger.valueOf(a);<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BigInteger&nbsp;B</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">BigInteger.valueOf(b);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;A^B</span><span style="COLOR: #008000"><br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BigInteger&nbsp;rA</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">BigInteger.valueOf(a);<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BigInteger&nbsp;rB</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">BigInteger.valueOf(b);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;store&nbsp;the&nbsp;result&nbsp;after&nbsp;computing</span><span style="COLOR: #008000"><br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.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;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">b;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;just&nbsp;b-1&nbsp;times</span><span style="COLOR: #008000"><br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rA</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">rA.multiply(A);<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.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;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;&nbsp;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">a;&nbsp;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)&nbsp;&nbsp;&nbsp;&nbsp;<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rB</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">rB.multiply(B);<br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(rA.subtract(rB));&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;sub</span><span style="COLOR: #008000"><br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br><img title="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" alt="SGU 112 a^b-b^a (Java Edition) - Icho - Brian Warehouse" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<p>附《Core Java I》里关于 大数的简单介绍，讲的算比较清晰的了：<br>Big Numbers<br>If the precision of the basic integer and floating-point types is not sufficient, you can<br>turn to a couple of handy classes in the java.math package: BigInteger and BigDecimal. These<br>are classes for manipulating numbers with an arbitrarily long sequence of digits. The<br>BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the<br>same for floating-point numbers.<br>Use the static valueOf method to turn an ordinary number into a big number:<br>BigInteger a = BigInteger.valueOf(100);<br>Unfortunately, you cannot use the familiar mathematical operators such as + and * to<br>combine big numbers. Instead, you must use methods such as add and multiply in the big<br>number classes.<br>BigInteger c = a.add(b); // c = a + b<br>BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2)<br>C++ NOTE: Unlike C++, Java has no programmable operator overloading. There was no way<br>for the programmer of the BigInteger class to redefine the + and * operators to give the add and<br>multiply operations of the BigInteger classes. The language designers did overload the + operator<br>to denote concatenation of strings. They chose not to overload other operators, and they<br>did not give Java programmers the opportunity to overload operators in their own classes.<br>Listing 3&#8211;6 shows a modification of the lottery odds program of Listing 3&#8211;5, updated to<br>work with big numbers. For example, if you are invited to participate in a lottery in<br>which you need to pick 60 numbers out of a possible 490 numbers, then this program<br>will tell you that your odds are 1 in 7163958434619955574151162225400929334117176<br>12789263493493351 013459481104668848. Good luck!<br>The program in Listing 3&#8211;5 computed the statement<br>lotteryOdds = lotteryOdds * (n - i + 1) / i;<br>When big numbers are used, the equivalent statement becomes<br>lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(BigInteger.valueOf(i));<br>Listing 3&#8211;6 BigIntegerTest.java<br>1. imp<wbr>ort java.math.*;<br>2. imp<wbr>ort java.util.*;<br>3.<br>4. /**<br>5. * This program uses big numbers to compute the odds of winning the grand prize in a lottery.<br>6. * @version 1.20 2004-02-10<br>7. * @author Cay Horstmann<br>8. */<br>9. public class BigIntegerTest<br>10. {<br>11. public static void main(String[] args)<br>12. {<br>13. Scanner in = new Scanner(System.in);<br>14.<br>15. System.out.print("How many numbers do you need to draw? ");<br>16. int k = in.nextInt();<br>17.<br>18. System.out.print("What is the highest number you can draw? ");<br>19. int n = in.nextInt();<br>20.<br>21. /*<br>22. * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)<br>23. */<br>24.<br>25. BigInteger lotteryOdds = BigInteger.valueOf(1);<br>26.<br>27. for (int i = 1; i &lt;= k; i++)<br>28. lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(<br>29. BigInteger.valueOf(i));<br>30.<br>31. System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");<br>32. }<br>33. }<br><br>java.math.BigInteger</p>
<p>? BigInteger add(BigInteger other)<br>? BigInteger subtract(BigInteger other)<br>? BigInteger multiply(BigInteger other)<br>? BigInteger divide(BigInteger other)<br>? BigInteger mod(BigInteger other)<br>returns the sum, difference, product, quotient, and remainder of this big integer and<br>other.<br>? int compareTo(BigInteger other)<br>returns 0 if this big integer equals other, a negative result if this big integer is less<br>than other, and a positive result otherwise.<br>? static BigInteger valueOf(long x)<br>returns a big integer whose value equals x.<br>? BigDecimal add(BigDecimal other)<br>? BigDecimal subtract(BigDecimal other)<br>? BigDecimal multiply(BigDecimal other)<br>? BigDecimal divide(BigDecimal other, RoundingMode mode) 5.0<br>returns the sum, difference, product, or quotient of this big decimal and other.<br>To compute the quotient, you must supply a rounding mode. The mode<br>RoundingMode.HALF_UP is the rounding mode that you learned in school (i.e., round<br>down digits 0 . . . 4, round up digits 5 . . . 9). It is appropriate for routine<br>calculations. See the API documentation for other rounding modes.<br>? int compareTo(BigDecimal other)<br>returns 0 if this big decimal equals other, a negative result if this big decimal is less<br>than other, and a positive result otherwise.<br>? static BigDecimal valueOf(long x)<br>? static BigDecimal valueOf(long x, int scale)<br>returns a big decimal whose value equals x or x /10scale.</p>
<img src ="http://www.cppblog.com/Icho/aggbug/123694.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Icho/" target="_blank">Brian</a> 2010-08-17 13:21 <a href="http://www.cppblog.com/Icho/archive/2010/08/17/123694.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SGU 分类开篇语</title><link>http://www.cppblog.com/Icho/archive/2010/08/17/123693.html</link><dc:creator>Brian</dc:creator><author>Brian</author><pubDate>Tue, 17 Aug 2010 05:17:00 GMT</pubDate><guid>http://www.cppblog.com/Icho/archive/2010/08/17/123693.html</guid><wfw:comment>http://www.cppblog.com/Icho/comments/123693.html</wfw:comment><comments>http://www.cppblog.com/Icho/archive/2010/08/17/123693.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Icho/comments/commentRss/123693.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Icho/services/trackbacks/123693.html</trackback:ping><description><![CDATA[别看了，老子疯了。SGU的进展肯定相当缓慢，但是我会用心做的。下面先转一个大牛的Sgu袖珍分类。<br>101 Domino 欧拉路<br>102 Coprime 枚举/数学方法<br>103 Traffic Lights 最短路<br>104 Little Shop of Flowers 动态规划<br>105 Div 3 找规律<br>106 The Equation 扩展欧几里德<br>107 987654321 Problem 找规律<br>108 Self-numbers II 枚举+筛法递推<br>109 Magic of David Copperfield II 构造<br>110 Dungeon 计算几何+模拟<br>111 Very Simple Problem 模拟笔算开方<br>112 a^b-b^a 高精度<br>113 Nearly Prime Numbers 判质数<br>114 Telecasting Station 找中位数<br>115 Calendar 模拟<br>116 Index of Super-prime 动态规划<br>117 Counting 快速幂<br>118 Digital Root 模拟<br>119 Magic Pairs 枚举<br>120 Archipelago 计算几何<br>121 Bridges Painting 构造<br>122 The Book 构造哈密顿回路<br>123 The Sum 递推<br>124 Broken line 计算几何<br>125 Shtirlits 搜索<br>126 Boxes 数学方法<br>127 Telephone directory 统计<br>128 Snake 并查集 + 树状数组<br>129 Inheritance 计算几何<br>130 Circle 卡特兰数<br>131 Hardwood floor 状态压缩动规<br>132 Another Chocolate Maniac 状态压缩动规<br>133 Border 贪心<br>134 Centroid 树型DP<br>135 Drawing Lines 找规律<br>136 Erasing Edges 数学方法<br>137 Funny Strings 构造<br>138 Games of Chess 构造<br>139 Help Needed! 数学方法<br>140 Integer Sequences 扩展欧几里德<br>141 Jumping Joe 扩展欧几里德<br>142 Keyword 枚举<br>143 Long Live the Queen 树型DP<br>144 Meeting 数学方法<br>145 Strange People 二分答案+搜索<br>146 The Runner 数学方法<br>147 Black-white King 枚举<br>148 B-Station 枚举+快排/二分/堆<br>149 Computer Network 树型DP<br>150 Mr. Beetle II 枚举<br>151 Construct a triangle 计算几何构造<br>152 Making round 构造<br>153 Playing With Matches 动态规划+找循环节<br>154 Factorial 二分 数学方法<br>155 Cartesian Tree 建笛卡尔树<br>156 Strange Graph 缩团+欧拉回路<br>157 Patience 搜索+开表<br>158 Commuter Train 枚举<br>159 Self-replicating Numbers 扩展队列+高精度<br>160 Magic Multiplying Machine 动态规划<br>161 Intuitionistic Logic 搜索*<br>162 Pyramids 数学方法<br>163 Wise King 模拟<br>164 Airlines 贪心<br>165 Basketball 构造<br>166 Editor 模拟<br>167 I-country 动态规划<br>168 Matrix 动态规划<br>169 Numbers 数学方法<br>170 Particles 贪心<br>171 Sarov zones 贪心<br>172 eXam 判断二分图<br>173 Coins 高斯消元<br>174 Wall 并查集+Hash<br>175 Encoding 搜索/动态规划<br>176 Flow construction 上下界网络流<br>177 Sqare 倒序染色<br>178 Chain 数学方法<br>179 Brackets light 找规律<br>180 Inversions 归并排序/高级数据结构<br>181 X-Sequence 找循环节<br>182 Open the Brackets 搜索<br>183 Painting the balls 动态规划优化<br>184 Patties 直接计算<br>185 Two shortest 网络流<br>186 The chain 贪心<br>187 Twist and whirl -- want to cheat 块状链表<br>188 Factory guard 数学方法<br>189 Perl-like Substr 模拟<br>190 Dominoes 二分图匹配<br>191 Exhibition 贪心<br>192 RGB 离散化 ＋ 统计<br>193 Chinese Girls' Amusement 数学方法 + 高精度<br>194 Reactor Cooling 网络流<br>195 New Year Bonus Grant 贪心<br>196 Matrix Multiplication 数学方法<br>197 Nice Patterns Strike Back 动态规划+矩阵<br>198 Get Out! 计算几何<br>199 Beautiful People 最长非降子序列<br>200 Cracking RSA 高斯消元<br>201 Non Absorbing DFA 动态规划<br>202 The Towers of Hanoi Revisited 动态规划构造<br>203 Hyperhuffman 贪心<br>204 Little Jumper 二分+数学方法*<br>205 Quantization Problem 动态规划
<img src ="http://www.cppblog.com/Icho/aggbug/123693.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Icho/" target="_blank">Brian</a> 2010-08-17 13:17 <a href="http://www.cppblog.com/Icho/archive/2010/08/17/123693.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>