﻿<?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++博客-Mato is No.1-随笔分类-组合数学</title><link>http://www.cppblog.com/MatoNo1/category/20159.html</link><description>Mato是一只超级大沙茶……但他一直以来都想成为各项比赛都No.1的神犇……</description><language>zh-cn</language><lastBuildDate>Mon, 22 Jul 2013 07:21:34 GMT</lastBuildDate><pubDate>Mon, 22 Jul 2013 07:21:34 GMT</pubDate><ttl>60</ttl><item><title>【AHOI2013复仇】向JZPKIL认输了</title><link>http://www.cppblog.com/MatoNo1/archive/2013/02/06/197761.html</link><dc:creator>Mato_No1</dc:creator><author>Mato_No1</author><pubDate>Wed, 06 Feb 2013 15:26:00 GMT</pubDate><guid>http://www.cppblog.com/MatoNo1/archive/2013/02/06/197761.html</guid><wfw:comment>http://www.cppblog.com/MatoNo1/comments/197761.html</wfw:comment><comments>http://www.cppblog.com/MatoNo1/archive/2013/02/06/197761.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/MatoNo1/comments/commentRss/197761.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MatoNo1/services/trackbacks/197761.html</trackback:ping><description><![CDATA[RT，<br />今天又优化了一下JZPKIL，用上了各种无耻的手段，仍然无法干掉后两个点，并且BZOJ上的总时间50s也无法实现（后两个点一个就要20s），<br />看来基于组合数的做法由于要枚举因数，确实不行&#8230;&#8230;<br />（注：后两个点是人工构造的猥琐数据，所有的N都是若干个小质数之积，因数个数都上千，有的甚至上万&#8230;&#8230;）<br /><br />认输了&#8230;&#8230;<br />Orz @sevenk<img src ="http://www.cppblog.com/MatoNo1/aggbug/197761.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MatoNo1/" target="_blank">Mato_No1</a> 2013-02-06 23:26 <a href="http://www.cppblog.com/MatoNo1/archive/2013/02/06/197761.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【AHOI2013复仇】ZJOI2010 Perm 排列计数</title><link>http://www.cppblog.com/MatoNo1/archive/2012/10/30/194104.html</link><dc:creator>Mato_No1</dc:creator><author>Mato_No1</author><pubDate>Tue, 30 Oct 2012 13:35:00 GMT</pubDate><guid>http://www.cppblog.com/MatoNo1/archive/2012/10/30/194104.html</guid><wfw:comment>http://www.cppblog.com/MatoNo1/comments/194104.html</wfw:comment><comments>http://www.cppblog.com/MatoNo1/archive/2012/10/30/194104.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/MatoNo1/comments/commentRss/194104.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/MatoNo1/services/trackbacks/194104.html</trackback:ping><description><![CDATA[<a title="原题地址" href="http://www.lydsy.com/JudgeOnline/problem.php?id=2111">原题地址</a><br />这是个超级大水题，我太沙茶了，想傻了N久&#8230;&#8230;后来才反应过来&#8230;&#8230;所以要写一下作为警示。<br /><br />首先这个序列就是一个堆&#8230;&#8230;<br />因此，问题也就是说N个结点，权值刚好取遍1~N的堆的总数&#8230;&#8230;<br />设结果为F[N]。设N个结点的堆，左子树有l个结点，右子树有r个结点（显然有l+r+1=N），则有<br />F[N]=C(N-1, l) * F[l] * F[r]<br />这个理解起来很容易囧&#8230;&#8230;因为根结点只能是1，左子树和右子树显然也都是堆，因此相当于在2~N中取l个数组成左子树，剩下的数组成右子树&#8230;&#8230;又因为不管取哪些数，左右子树的组成方法总数都是F[l]、F[r]（只与次序有关）&#8230;&#8230;这样就得到上面的式子了囧&#8230;&#8230;<br />C(N-1, l)=N! / l! / r!，因此需要预处理出来A[i] = i! mod P，然后除法用逆元就行了囧&#8230;&#8230;<br /><br />不过，本沙茶一开始想按照层数枚举，然后相乘&#8230;&#8230;自然搞不出来囧&#8230;&#8230;后来又用暴力把N&lt;=15的结果拿出来分析，想找到规律&#8230;&#8230;结果毫无规律&#8230;&#8230;后来又纠结了N久才想到上面这个&#8230;&#8230;真正比赛的时候就悲剧了囧&#8230;&#8230;所以要警示一下&#8230;&#8230;<br /><br />代码：<br /><div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%;"><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 />#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">stdlib.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">.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 /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;re(i,&nbsp;n)&nbsp;for&nbsp;(int&nbsp;i=0;&nbsp;i&lt;n;&nbsp;i++)</span><span style="color: #000000;"><br /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;re1(i,&nbsp;n)&nbsp;for&nbsp;(int&nbsp;i=1;&nbsp;i&lt;=n;&nbsp;i++)</span><span style="color: #000000;"><br /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;re2(i,&nbsp;l,&nbsp;r)&nbsp;for&nbsp;(int&nbsp;i=l;&nbsp;i&lt;r;&nbsp;i++)</span><span style="color: #000000;"><br /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;re3(i,&nbsp;l,&nbsp;r)&nbsp;for&nbsp;(int&nbsp;i=l;&nbsp;i&lt;=r;&nbsp;i++)</span><span style="color: #000000;"><br /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;rre(i,&nbsp;n)&nbsp;for&nbsp;(int&nbsp;i=n-1;&nbsp;i&gt;=0;&nbsp;i--)</span><span style="color: #000000;"><br /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;rre1(i,&nbsp;n)&nbsp;for&nbsp;(int&nbsp;i=n;&nbsp;i&gt;0;&nbsp;i--)</span><span style="color: #000000;"><br /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;rre2(i,&nbsp;r,&nbsp;l)&nbsp;for&nbsp;(int&nbsp;i=r-1;&nbsp;i&gt;=l;&nbsp;i--)</span><span style="color: #000000;"><br /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;rre3(i,&nbsp;r,&nbsp;l)&nbsp;for&nbsp;(int&nbsp;i=r;&nbsp;i&gt;=l;&nbsp;i--)</span><span style="color: #000000;"><br /></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;ll&nbsp;long&nbsp;long</span><span style="color: #000000;"><br /></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;">1000010</span><span style="color: #000000;">,&nbsp;INF&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">~</span><span style="color: #000000;">0U</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">;<br /></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n;<br />ll&nbsp;MOD,&nbsp;A[MAXN],&nbsp;F[MAXN],&nbsp;res;<br /></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;init()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;cin&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;n&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;MOD;<br />}<br /></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;prepare()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;A[</span><span style="color: #000000;">0</span><span style="color: #000000;">]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;A[</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: #000000;">1</span><span style="color: #000000;">;&nbsp;re3(i,&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">,&nbsp;n)&nbsp;A[i]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(A[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;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;i)&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;MOD;<br />}<br /></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;exgcd(ll&nbsp;a,&nbsp;ll&nbsp;b,&nbsp;ll&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">x,&nbsp;ll&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">y)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(b)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ll&nbsp;_x,&nbsp;_y;&nbsp;exgcd(b,&nbsp;a&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;b,&nbsp;_x,&nbsp;_y);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;_y;&nbsp;y&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;_x&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;(a&nbsp;</span><span style="color: #000000;">/</span><span style="color: #000000;">&nbsp;b)&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;_y;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;{x&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;&nbsp;y&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">;}<br />}<br /></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;solve()<br />{<br />&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;F[</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: #000000;">1</span><span style="color: #000000;">;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;s&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</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;">,&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;">;&nbsp;ll&nbsp;x,&nbsp;y;<br />&nbsp;&nbsp;&nbsp;&nbsp;re3(i,&nbsp;</span><span style="color: #000000;">2</span><span style="color: #000000;">,&nbsp;n)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(l&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;s)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(r&nbsp;</span><span style="color: #000000;">==</span><span style="color: #000000;">&nbsp;s)&nbsp;{s&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;s&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;&nbsp;l</span><span style="color: #000000;">++</span><span style="color: #000000;">;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;r</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span><span style="color: #0000ff;">else</span><span style="color: #000000;">&nbsp;l</span><span style="color: #000000;">++</span><span style="color: #000000;">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F[i]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;F[l]&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;F[r]&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;MOD;&nbsp;F[i]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;F[i]&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;A[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;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;MOD;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exgcd(A[l],&nbsp;MOD,&nbsp;x,&nbsp;y);&nbsp;F[i]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;F[i]&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;MOD;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(F[i]&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;F[i]&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;MOD;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exgcd(A[r],&nbsp;MOD,&nbsp;x,&nbsp;y);&nbsp;F[i]&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;F[i]&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #000000;">%</span><span style="color: #000000;">&nbsp;MOD;&nbsp;</span><span style="color: #0000ff;">if</span><span style="color: #000000;">&nbsp;(F[i]&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">)&nbsp;F[i]&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;MOD;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;res&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;F[n];<br />}<br /></span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;pri()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;res&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br />}<br /></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;init();<br />&nbsp;&nbsp;&nbsp;&nbsp;prepare();<br />&nbsp;&nbsp;&nbsp;&nbsp;solve();<br />&nbsp;&nbsp;&nbsp;&nbsp;pri();<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></div><img src ="http://www.cppblog.com/MatoNo1/aggbug/194104.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/MatoNo1/" target="_blank">Mato_No1</a> 2012-10-30 21:35 <a href="http://www.cppblog.com/MatoNo1/archive/2012/10/30/194104.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>