index.html 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 4.2.0"><link rel="apple-touch-icon" sizes="180x180" href="/blog/blog/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/blog/blog/images/favicon-frog.png"><link rel="icon" type="image/png" sizes="16x16" href="/blog/blog/images/favicon-frog.png"><link rel="mask-icon" href="/blog/blog/images/logo.svg" color="#222"><link rel="stylesheet" href="/blog/css/main.css"><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Comic Sans MS:300,300italic,400,400italic,700,700italic|Consolas:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext"><link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css"><link rel="stylesheet" href="/blog/lib/pace/pace-theme-minimal.min.css"><script src="/blog/lib/pace/pace.min.js"></script><script id="hexo-configurations">var NexT=window.NexT||{},CONFIG={hostname:"schtonn.github.io",root:"/blog/",scheme:"Muse",version:"7.8.0",exturl:!1,sidebar:{position:"left",display:"post",padding:18,offset:12,onmobile:!1},copycode:{enable:!0,show_result:!0,style:"flat"},back2top:{enable:!0,sidebar:!1,scrollpercent:!0},bookmark:{enable:!1,color:"#222",save:"auto"},fancybox:!1,mediumzoom:!1,lazyload:!0,pangu:!1,comments:{style:"tabs",active:"valine",storage:!0,lazyload:!1,nav:null,activeClass:"valine"},algolia:{hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}},localsearch:{enable:!1,trigger:"auto",top_n_per_article:1,unescape:!1,preload:!1},motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}}}</script><meta name="description" content="前置知识 动态规划,二进制基础知识。 引入 假设我们有一个矩形的棋盘,里面有 \(n\) 行 \(m\) 列的 \(1\times1\) 的方格,现在我们要往里面放 \(1\times2\) 的多米诺骨牌。要求骨牌不能重叠,正好填满棋盘的所有方格。求所有方案数。 这时我们可以利用状态压缩,对每一层进行动态规划。"><meta property="og:type" content="article"><meta property="og:title" content="各种奇奇怪怪的多米诺骨牌"><meta property="og:url" content="https://schtonn.github.io/blog/posts/domino/index.html"><meta property="og:site_name" content="schtonn"><meta property="og:description" content="前置知识 动态规划,二进制基础知识。 引入 假设我们有一个矩形的棋盘,里面有 \(n\) 行 \(m\) 列的 \(1\times1\) 的方格,现在我们要往里面放 \(1\times2\) 的多米诺骨牌。要求骨牌不能重叠,正好填满棋盘的所有方格。求所有方案数。 这时我们可以利用状态压缩,对每一层进行动态规划。"><meta property="og:locale" content="en_US"><meta property="og:image" content="https://schtonn.github.io/blog/images/domino-1.png"><meta property="article:published_time" content="2020-06-21T04:22:19.000Z"><meta property="article:modified_time" content="2022-10-19T15:02:06.670Z"><meta property="article:author" content="Alex"><meta property="article:tag" content="dp"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://schtonn.github.io/blog/images/domino-1.png"><link rel="canonical" href="https://schtonn.github.io/blog/posts/domino/"><script id="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0,lang:"en"}</script><title>各种奇奇怪怪的多米诺骨牌 | schtonn</title><noscript><style>.sidebar-inner,.use-motion .brand,.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line-before i{left:initial}.use-motion .logo-line-after i{right:initial}</style></noscript></head><body itemscope itemtype="http://schema.org/WebPage"><div class="container use-motion"><div class="headband"></div><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-nav-toggle"><div class="toggle" aria-label="Toggle navigation bar"><span class="toggle-line toggle-line-first"></span><span class="toggle-line toggle-line-middle"></span><span class="toggle-line toggle-line-last"></span></div></div><div class="site-meta"><a href="/blog/" class="brand" rel="start"><span class="logo-line-before"><i></i></span><h1 class="site-title">schtonn</h1><span class="logo-line-after"><i></i></span></a><p class="site-subtitle" itemprop="description">schtonn</p></div><div class="site-nav-right"><div class="toggle popup-trigger"></div></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i> Home</a></li><li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="fa fa-fw fa-tags"></i> Tags</a></li><li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i> Archives</a></li><li class="menu-item menu-item-games"><a href="/blog/games/" rel="section"><i class="fa fa-fw fa-gamepad"></i> Games</a></li></ul></nav></div></header><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span>0%</span></div><main class="main"><div class="main-inner"><div class="content-wrap"><div class="content post posts-expand"><article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en"><link itemprop="mainEntityOfPage" href="https://schtonn.github.io/blog/posts/domino/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/blog/images/avatar.gif"><meta itemprop="name" content="Alex"><meta itemprop="description" content="blog"></span><script type="text/javascript" src="/blog/js/md5.js"></script><script></script><script>document.oncopy=function(e){window.event&&(e=window.event);try{var t=e.srcElement;return"INPUT"==t.tagName&&"text"==t.type.toLowerCase()||"TEXTAREA"==t.tagName}catch(e){return!1}}</script><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="schtonn"></span><header class="post-header"><h1 class="post-title" itemprop="name headline"> 各种奇奇怪怪的多米诺骨牌</h1><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i></span> <span class="post-meta-item-text">Posted on</span> <time title="Created: 2020-Jun-21 12:22:19" itemprop="dateCreated datePublished" datetime="2020-06-21T12:22:19+08:00">2020-Jun-21</time></span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-check-o"></i></span> <span class="post-meta-item-text">Edited on</span> <time title="Modified: 2022-Oct-19 23:02:06" itemprop="dateModified" datetime="2022-10-19T23:02:06+08:00">2022-Oct-19</time></span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-comment-o"></i></span> <span class="post-meta-item-text">Valine:</span><a title="valine" href="/blog/posts/domino/#valine-comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/blog/posts/domino/" itemprop="commentCount"></span></a></span></div></header><div class="post-body" itemprop="articleBody"><h2 id="前置知识">前置知识</h2><p>动态规划,二进制基础知识。</p><h2 id="引入">引入</h2><p>假设我们有一个矩形的棋盘,里面有 <span class="math inline">\(n\)</span> 行 <span class="math inline">\(m\)</span> 列的 <span class="math inline">\(1\times1\)</span> 的方格,现在我们要往里面放 <span class="math inline">\(1\times2\)</span> 的多米诺骨牌。要求骨牌不能重叠,正好填满棋盘的所有方格。求所有方案数。</p><p>这时我们可以利用状态压缩,对每一层进行动态规划。</p><a id="more"></a><h2 id="状态">状态</h2><p>对于棋盘上的每一个位置,如果<strong>该位置上的骨牌覆盖了其下的方格</strong>,那么该位置上的状态为1,否则为0。</p><p>对于已经覆盖完毕的一整行,将该行每一个位置的状态整合成一个二进制数,这就是这一行的状态。</p><p>例如:</p><figure> <img data-src="/blog/images/domino-1.png" alt="一种覆盖情况"><figcaption>一种覆盖情况</figcaption></figure><p>在当前情况下,第一行的状态是 <span class="math inline">\((001)_2\)</span>,第二行的状态是 <span class="math inline">\((110)_2\)</span>。</p><p>对于状态压缩的进一步讲述,在<a href="/blog/posts/hamilton/#状态压缩">另一篇文章</a>中,这里不多赘述。</p><h2 id="状态转移">状态转移</h2><p>对于每一个层,状态转移就是看这一层的某一种状态能不能转移到下一层的某一种状态。例如上图的第一行和第二行,就是状态 <span class="math inline">\((001)_2\)</span>,转移到了状态 <span class="math inline">\((110)_2\)</span>。这就是一次成功的状态转移。</p><p>更进一步的描述,我们需要<strong>枚举这一行的每一种状态,以及上一行的每一种状态,检查上一行的状态能否转移到这一行,并且要求上一行的状态是可达的。</strong></p><h3 id="检查">检查</h3><p>这时我们就需要一个函数来检查上一行的状态能否转移到这一行。</p><ul><li><p>我们假设上一行的状态为A,本行的状态为B。</p></li><li><p>首先检查A,B是否有重叠,如果有那么立即退出。</p></li><li><p>如果该位置的骨牌覆盖了下一行,那么它一定是竖向放置的。所以设所有竖向放置的骨牌C为A按位或B。</p></li><li><p>将C取反,结果D即为横向放置的骨牌。D的二进制位中应该是两位一组的。</p><p>因为 <span class="math inline">\(3=(11)_2\)</span>,所以E=D/3标示了每个两位一组的二进制位中右边一位。如: <span class="math display">\[\frac{(1101111)_2}{3}=(0100101)_2\]</span></p></li><li><p>E按位或E&lt;&lt;1应等于D,因为所有标示都左移了一位,并与原来的按位或。</p></li></ul><p>这就是检查函数。</p><h3 id="代码">代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"bits/stdc++.h"</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">1</span>&lt;&lt;<span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod=<span class="number">10007</span>;</span><br><span class="line"><span class="keyword">int</span> m,n;</span><br><span class="line"><span class="keyword">int</span> F[<span class="number">110</span>][N+<span class="number">10</span>];<span class="comment">//F[i][j]表示骨牌覆盖完前i行,多余的骨牌状态为j的方案总数。</span></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">T</span><span class="params">(<span class="keyword">int</span> A,<span class="keyword">int</span> B)</span></span>&#123;</span><br><span class="line"> <span class="keyword">if</span>(A&amp;B)<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">int</span> C=A|B;</span><br><span class="line"> <span class="keyword">int</span> D=C^((<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>);</span><br><span class="line"> <span class="keyword">int</span> E=D/<span class="number">3</span>;</span><br><span class="line"> <span class="keyword">int</span> F=E|(E&lt;&lt;<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(F==D)<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"> <span class="built_in">cin</span>&gt;&gt;n&gt;&gt;m;</span><br><span class="line"> F[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++)&#123;<span class="comment">//行数</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> cur=<span class="number">0</span>;cur&lt;=(<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>;cur++)&#123;<span class="comment">//枚举本行状态</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> pre=<span class="number">0</span>;pre&lt;=(<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>;pre++)&#123;<span class="comment">//枚举上一行状态</span></span><br><span class="line"> <span class="keyword">int</span> k=T(pre,cur);</span><br><span class="line"> F[i][cur]=(F[i][cur]+F[i<span class="number">-1</span>][pre]*k)%mod;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="built_in">cout</span>&lt;&lt;F[m][<span class="number">0</span>]&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="拓展">拓展</h2><h3 id="破洞">破洞</h3><p>对于棋盘上有破洞的情况,可以在状态转移的时候使用按位或,这样破洞上就不会放骨牌了。</p><h4 id="代码-1">代码</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"bits/stdc++.h"</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">1</span>&lt;&lt;<span class="number">10</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod=<span class="number">10007</span>;</span><br><span class="line"><span class="keyword">int</span> m,n,p;</span><br><span class="line"><span class="keyword">int</span> F[<span class="number">100</span>][N];<span class="comment">//F[i][j]表示骨牌覆盖完前i行,多余的骨牌状态为j的方案总数。</span></span><br><span class="line"><span class="keyword">int</span> hole[<span class="number">100</span>];</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">T</span><span class="params">(<span class="keyword">int</span> A,<span class="keyword">int</span> B)</span></span>&#123;</span><br><span class="line"> <span class="keyword">int</span> C=A|B;</span><br><span class="line"> <span class="keyword">int</span> D=C^((<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>);</span><br><span class="line"> <span class="keyword">int</span> E=D/<span class="number">3</span>;</span><br><span class="line"> <span class="keyword">int</span> F=E|(E&lt;&lt;<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(A&amp;B)<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">if</span>(F==D)<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"> <span class="built_in">cin</span>&gt;&gt;n&gt;&gt;m&gt;&gt;p;</span><br><span class="line"> <span class="keyword">int</span> x,y;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=p;i++)&#123;</span><br><span class="line"> <span class="built_in">cin</span>&gt;&gt;x&gt;&gt;y;</span><br><span class="line"> hole[y]|=<span class="number">1</span>&lt;&lt;(x<span class="number">-1</span>);</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">if</span>(m==<span class="number">2</span>)&#123;</span><br><span class="line"> F[<span class="number">0</span>][hole[<span class="number">1</span>]]=<span class="number">1</span>;</span><br><span class="line"> &#125;<span class="keyword">else</span>&#123;</span><br><span class="line"> F[<span class="number">0</span>][<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++)&#123;<span class="comment">//行数</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> B=<span class="number">0</span>;B&lt;=(<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>;B++)&#123;<span class="comment">//枚举本行状态</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> A=<span class="number">0</span>;A&lt;=(<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>;A++)&#123;<span class="comment">//枚举上一行状态</span></span><br><span class="line"> <span class="keyword">int</span> k=T(A,B|hole[i]);</span><br><span class="line"> F[i][B]=(F[i][B]+F[i<span class="number">-1</span>][A]*k)%mod;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="built_in">cout</span>&lt;&lt;F[m][<span class="number">0</span>]&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="大数据">大数据</h3><p>如果n小一些,m大亿些,那么可以使用矩阵快速幂,因为每一层之间的转移本质上都是一样的,所以用可以矩阵来代替转移函数。</p><p>如果m再大亿些,那么就需要在初始化时加入一点精髓。</p><p>矩阵中有的转移可能是永远也用不到的,因为到达不了它的前置状态。这时我们可以用一个数组来记录能到达的状态,只对能到达的状态进行计算。</p><h4 id="代码-2">代码</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"bits/stdc++.h"</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> N=<span class="number">1</span>&lt;&lt;<span class="number">5</span>;</span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> mod=<span class="number">10007</span>;</span><br><span class="line"><span class="keyword">int</span> m,n;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">T</span><span class="params">(<span class="keyword">int</span> A,<span class="keyword">int</span> B)</span></span>&#123;</span><br><span class="line"> <span class="keyword">if</span>(A&amp;B)<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">int</span> C=A|B;</span><br><span class="line"> <span class="keyword">int</span> D=C^((<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>);</span><br><span class="line"> <span class="keyword">int</span> E=D/<span class="number">3</span>;</span><br><span class="line"> <span class="comment">// cout&lt;&lt;fact(A)&lt;&lt;','&lt;&lt;fact(B)&lt;&lt;','&lt;&lt;fact(C)&lt;&lt;','&lt;&lt;fact(D)&lt;&lt;','&lt;&lt;fact(E);</span></span><br><span class="line"> <span class="keyword">int</span> F=E|(E&lt;&lt;<span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span>(F==D)<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">matrix</span>&#123;</span></span><br><span class="line"> <span class="keyword">int</span> v[N+<span class="number">2</span>][N+<span class="number">2</span>];</span><br><span class="line"> <span class="keyword">int</span> x,y;</span><br><span class="line">&#125;t,I,ans;</span><br><span class="line"><span class="keyword">int</span> pre[N+<span class="number">2</span>];</span><br><span class="line">ostream&amp;<span class="keyword">operator</span>&lt;&lt;(ostream&amp;ous,matrix a)&#123;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;a.x;i++)&#123;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;a.y;j++)&#123;</span><br><span class="line"> ous&lt;&lt;a.v[i][j]&lt;&lt;<span class="string">' '</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> ous&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> ous;</span><br><span class="line">&#125;</span><br><span class="line">matrix <span class="keyword">operator</span>+(matrix a,matrix b)&#123;</span><br><span class="line"> matrix c;</span><br><span class="line"> <span class="keyword">if</span>(a.x!=b.x||a.y!=b.y)<span class="keyword">return</span> c;</span><br><span class="line"> <span class="keyword">int</span> x=a.x,y=a.y;</span><br><span class="line"> c.x=x;</span><br><span class="line"> c.y=y;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;x;i++)&#123;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;y;j++)&#123;</span><br><span class="line"> c.v[i][j]=(a.v[i][j]+b.v[i][j])%mod;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> c;</span><br><span class="line">&#125;</span><br><span class="line">matrix <span class="keyword">operator</span>*(matrix a,matrix b)&#123;</span><br><span class="line"> matrix c;</span><br><span class="line"> <span class="keyword">if</span>(a.y!=b.x)<span class="keyword">return</span> c;</span><br><span class="line"> <span class="keyword">int</span> x=a.x,y=b.y,z=a.y;</span><br><span class="line"> c.x=x;</span><br><span class="line"> c.y=y;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;x;i++)&#123;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;y;j++)&#123;</span><br><span class="line"> c.v[i][j]=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> k=<span class="number">0</span>;k&lt;z;k++)&#123;</span><br><span class="line"> c.v[i][j]=(c.v[i][j]+a.v[i][k]*b.v[k][j]+mod)%mod;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="keyword">return</span> c;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span>&#123;</span><br><span class="line"> t.x=t.y=<span class="number">1</span>&lt;&lt;n;</span><br><span class="line"> I.x=I.y=<span class="number">1</span>&lt;&lt;n;</span><br><span class="line"> pre[<span class="number">0</span>]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;=(<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>;i++)&#123;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;=(<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>;j++)&#123;</span><br><span class="line"> <span class="keyword">if</span>(i==j) I.v[i][j]=<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">if</span>(pre[i]||pre[j])&#123;</span><br><span class="line"> t.v[i][j]=T(i,j);</span><br><span class="line"> <span class="keyword">if</span>(t.v[i][j])pre[i]=pre[j]=<span class="number">1</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"> <span class="built_in">cin</span>&gt;&gt;n&gt;&gt;m;</span><br><span class="line"> init();</span><br><span class="line"> <span class="keyword">int</span> cnt=m;</span><br><span class="line"> ans=I;</span><br><span class="line"> <span class="keyword">while</span>(cnt)&#123;</span><br><span class="line"> <span class="keyword">if</span>(cnt&amp;<span class="number">1</span>)ans=ans*t;</span><br><span class="line"> t=t*t;</span><br><span class="line"> cnt&gt;&gt;=<span class="number">1</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="built_in">cout</span>&lt;&lt;ans.v[<span class="number">0</span>][<span class="number">0</span>]%mod&lt;&lt;<span class="built_in">endl</span>;</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="我踩的坑">我踩的坑</h2><p>由于计算机中使用补码计算,所以对C取反的时候要精细考虑。</p><p>我一开始使用了 <code>~</code> 按位取反,它把整个二进制串都取反了,会出现奇奇怪怪的问题,这时我就想到了按位异或 <code>^</code>,可以只取反二进制中的一部分。</p><p>也就是这一部分代码:</p><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> D=C^((<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>);</span><br></pre></td></tr></table></figure></div><div><ul class="post-copyright"><li class="post-copyright-author"> <strong>Post author:</strong> Alex</li><li class="post-copyright-link"> <strong>Post link:</strong> <a href="https://schtonn.github.io/blog/posts/domino/" title="各种奇奇怪怪的多米诺骨牌">https://schtonn.github.io/blog/posts/domino/</a></li><li class="post-copyright-license"> <strong>Copyright Notice:</strong> All articles in this blog are licensed under<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i> BY-NC-SA</a> unless stating additionally.</li></ul></div><footer class="post-footer"><div class="post-tags"><a href="/blog/tags/dp/" rel="tag"><i class="fa fa-tag"></i> dp</a></div><div class="post-nav"><div class="post-nav-item"><a href="/blog/posts/manacher/" rel="prev" title="Manacher"><i class="fa fa-chevron-left"></i> Manacher</a></div><div class="post-nav-item"> <a href="/blog/posts/num-convert/" rel="next" title="进制转换">进制转换<i class="fa fa-chevron-right"></i></a></div></div></footer></article></div><div class="comments" id="valine-comments"></div><script>
  2. window.addEventListener('tabs:register', () => {
  3. let { activeClass } = CONFIG.comments;
  4. if (CONFIG.comments.storage) {
  5. activeClass = localStorage.getItem('comments_active') || activeClass;
  6. }
  7. if (activeClass) {
  8. let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
  9. if (activeTab) {
  10. activeTab.click();
  11. }
  12. }
  13. });
  14. if (CONFIG.comments.storage) {
  15. window.addEventListener('tabs:click', event => {
  16. if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
  17. let commentClass = event.target.classList[1];
  18. localStorage.setItem('comments_active', commentClass);
  19. });
  20. }
  21. </script></div><div class="toggle sidebar-toggle"><span class="toggle-line toggle-line-first"></span><span class="toggle-line toggle-line-middle"></span><span class="toggle-line toggle-line-last"></span></div><aside class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc"> Table of Contents</li><li class="sidebar-nav-overview"> Overview</li></ul><div class="post-toc-wrap sidebar-panel"><div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前置知识"><span class="nav-number">1.</span> <span class="nav-text">前置知识</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#引入"><span class="nav-number">2.</span> <span class="nav-text">引入</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#状态"><span class="nav-number">3.</span> <span class="nav-text">状态</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#状态转移"><span class="nav-number">4.</span> <span class="nav-text">状态转移</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#检查"><span class="nav-number">4.1.</span> <span class="nav-text">检查</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#代码"><span class="nav-number">4.2.</span> <span class="nav-text">代码</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#拓展"><span class="nav-number">5.</span> <span class="nav-text">拓展</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#破洞"><span class="nav-number">5.1.</span> <span class="nav-text">破洞</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#代码-1"><span class="nav-number">5.1.1.</span> <span class="nav-text">代码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#大数据"><span class="nav-number">5.2.</span> <span class="nav-text">大数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#代码-2"><span class="nav-number">5.2.1.</span> <span class="nav-text">代码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#我踩的坑"><span class="nav-number">6.</span> <span class="nav-text">我踩的坑</span></a></li></ol></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><p class="site-author-name" itemprop="name">Alex</p><div class="site-description" itemprop="description">blog</div></div><div class="site-state-wrap motion-element"><nav class="site-state"><div class="site-state-item site-state-posts"> <a href="/blog/archives"><span class="site-state-item-count">35</span> <span class="site-state-item-name">posts</span></a></div><div class="site-state-item site-state-tags"> <a href="/blog/tags/"><span class="site-state-item-count">8</span> <span class="site-state-item-name">tags</span></a></div></nav></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/schtonn" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;schtonn" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i> GitHub</a></span><span class="links-of-author-item"><a href="mailto:schtonn@163.com" title="E-Mail → mailto:schtonn@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i> E-Mail</a></span></div><div class="links-of-blogroll motion-element"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-link"></i> Links</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"> <a href="https://yonghong.github.io/" title="https:&#x2F;&#x2F;yonghong.github.io" rel="noopener" target="_blank">Yonghong</a></li><li class="links-of-blogroll-item"> <a href="https://source.unsplash.com/random/1600x900" title="https:&#x2F;&#x2F;source.unsplash.com&#x2F;random&#x2F;1600x900" rel="noopener" target="_blank">Background</a></li></ul><iframe width="400" height="300" frameborder="0" src="https://cdn.abowman.com/widgets/treefrog/index.html?up_bodyColor=2d2d2d&up_pattern=0&up_patternColor=000000&up_footColor=2d2d2d&up_eyeColor=3a3a3a&up_bellySize=50&up_backgroundColor=222222&up_tongueColor=2b2d2d&up_flyColor=3a3a3a&up_releaseFly=0"></iframe></div></div></div></aside><div id="sidebar-dimmer"></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright"> &copy; 2019 – <span itemprop="copyrightYear">2023</span><span class="with-love"><i class="fa fa-user"></i></span> <span class="author" itemprop="copyrightHolder">Alexander</span></div></div></footer></div><script src="/blog/lib/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script><script src="/blog/lib/velocity/velocity.min.js"></script><script src="/blog/lib/velocity/velocity.ui.min.js"></script><script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script><script src="/blog/js/schemes/muse.js"></script><script src="/blog/js/next-boot.js"></script><script>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}()</script><script>
  22. if (typeof MathJax === 'undefined') {
  23. window.MathJax = {
  24. loader: {
  25. load: ['[tex]/mhchem'],
  26. source: {
  27. '[tex]/amsCd': '[tex]/amscd',
  28. '[tex]/AMScd': '[tex]/amscd'
  29. }
  30. },
  31. tex: {
  32. inlineMath: {'[+]': [['$', '$']]},
  33. packages: {'[+]': ['mhchem']},
  34. tags: 'ams'
  35. },
  36. options: {
  37. renderActions: {
  38. findScript: [10, doc => {
  39. document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
  40. const display = !!node.type.match(/; *mode=display/);
  41. const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
  42. const text = document.createTextNode('');
  43. node.parentNode.replaceChild(text, node);
  44. math.start = {node: text, delim: '', n: 0};
  45. math.end = {node: text, delim: '', n: 0};
  46. doc.math.push(math);
  47. });
  48. }, '', false],
  49. insertedScript: [200, () => {
  50. document.querySelectorAll('mjx-container').forEach(node => {
  51. let target = node.parentNode;
  52. if (target.nodeName.toLowerCase() === 'li') {
  53. target.parentNode.classList.add('has-jax');
  54. }
  55. });
  56. }, '', false]
  57. }
  58. }
  59. };
  60. (function () {
  61. var script = document.createElement('script');
  62. script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
  63. script.defer = true;
  64. document.head.appendChild(script);
  65. })();
  66. } else {
  67. MathJax.startup.document.state(0);
  68. MathJax.texReset();
  69. MathJax.typeset();
  70. }
  71. </script><script>
  72. NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  73. NexT.utils.getScript('https://cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', () => {
  74. var GUEST = ['nick', 'mail', 'link'];
  75. var guest = 'nick,mail';
  76. guest = guest.split(',').filter(item => {
  77. return GUEST.includes(item);
  78. });
  79. new Valine({
  80. el : '#valine-comments',
  81. verify : false,
  82. notify : false,
  83. appId : 'BmologYYnRqCv0SLHDeDdA17-gzGzoHsz',
  84. appKey : 'w9mVebFMdCmY6Nh9vfcBGaGt',
  85. placeholder: "Comment...",
  86. avatar : 'mp',
  87. meta : guest,
  88. pageSize : '10' || 10,
  89. visitor : false,
  90. lang : 'en' || 'zh-cn',
  91. path : location.pathname,
  92. recordIP : true,
  93. serverURLs : ''
  94. });
  95. }, window.Valine);
  96. });
  97. </script></body></html>