<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
      <title>Jia Zhang</title>
      <link>https://jiaz.dev</link>
      <description>Software Engineer. Systems &amp; Performance.</description>
      <generator>Zola</generator>
      <language>en</language>
      <atom:link href="https://jiaz.dev/rss.xml" rel="self" type="application/rss+xml"/>
      <lastBuildDate>Sun, 08 Nov 2026 00:00:00 +0000</lastBuildDate>
      <item>
          <title>Value Categories in C++</title>
          <pubDate>Tue, 23 Aug 2022 00:00:00 +0000</pubDate>
          <author>Jia Zhang</author>
          <link>https://jiaz.dev/blog/value-categories-in-cpp/</link>
          <guid>https://jiaz.dev/blog/value-categories-in-cpp/</guid>
          <description xml:base="https://jiaz.dev/blog/value-categories-in-cpp/">&lt;p&gt;Three macros are enough to ask the compiler for the value category of any expression:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;type_traits&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; IS_LVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;is_lvalue_reference&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;decltype&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;{}&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; IS_XVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;  (&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;is_rvalue_reference&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;decltype&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;{}&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;define&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; IS_PRVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;is_reference&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;decltype&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;expr&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;{}&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By the end of this post it should be clear why they work, and why the doubled parentheses are not a typo.&lt;/p&gt;
&lt;h2 id=&quot;categories-belong-to-expressions&quot;&gt;Categories belong to expressions&lt;a class=&quot;anchor&quot; href=&quot;#categories-belong-to-expressions&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The single most useful fact about value categories is that they describe &lt;em&gt;expressions&lt;/em&gt;, not values or objects. The variable &lt;code&gt;x&lt;/code&gt; is not an lvalue; the expression &lt;code&gt;x&lt;/code&gt; is. The same object can be reached through an lvalue in one place and an xvalue in another.&lt;/p&gt;
&lt;p&gt;Every expression has exactly one of three primary categories:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;an &lt;strong&gt;lvalue&lt;/strong&gt;, such as &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;*p&lt;/code&gt; or &lt;code&gt;&quot;hello&quot;&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;prvalue&lt;/strong&gt; (“pure rvalue”), such as &lt;code&gt;3&lt;/code&gt;, &lt;code&gt;x + 1&lt;/code&gt; or &lt;code&gt;std::string(&quot;hi&quot;)&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;an &lt;strong&gt;xvalue&lt;/strong&gt; (“expiring value”), such as &lt;code&gt;std::move(x)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Two umbrella terms group them: a &lt;em&gt;glvalue&lt;/em&gt; is an lvalue or an xvalue, and an &lt;em&gt;rvalue&lt;/em&gt; is a prvalue or an xvalue.&lt;/p&gt;
&lt;p&gt;The old intuition still works for the first split. You can write &lt;code&gt;x = 3&lt;/code&gt; but not &lt;code&gt;3 = x&lt;/code&gt;, so &lt;code&gt;x&lt;/code&gt;, which may appear on the left of an assignment, is an lvalue, and &lt;code&gt;3&lt;/code&gt; is not. The modern categories add a second question. A glvalue &lt;em&gt;refers to&lt;/em&gt; an object or function that already exists, so it has an identity. A prvalue does not refer to anything; it is a recipe for a value, and it produces an object only when it is used to initialize one. Among the glvalues, the xvalues are the ones whose resources may be taken, because the code has declared that it no longer needs them.&lt;/p&gt;
&lt;h2 id=&quot;temporary-materialization&quot;&gt;Temporary materialization&lt;a class=&quot;anchor&quot; href=&quot;#temporary-materialization&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The case that makes xvalues click is temporary materialization:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; S&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; S&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;S()&lt;/code&gt; is a prvalue, and no &lt;code&gt;S&lt;/code&gt; object exists yet. Member access, however, needs an object to reach into. In the C++17 model, the compiler resolves this by &lt;em&gt;materializing&lt;/em&gt; a temporary: it creates an &lt;code&gt;S&lt;/code&gt; from the prvalue and gives you an xvalue that refers to it. &lt;code&gt;S().m&lt;/code&gt; is therefore an xvalue as well. The same conversion happens whenever a prvalue meets something that needs an object, such as a reference binding or a call to a member function. The &lt;a rel=&quot;external&quot; href=&quot;https://en.cppreference.com/cpp/language/implicit_conversion#Temporary_materialization&quot;&gt;cppreference page on implicit conversions&lt;/a&gt; spells out the rule.&lt;/p&gt;
&lt;p&gt;This is the real distinction between the two kinds of rvalue. A prvalue is not an object until something needs one. An xvalue names an object that exists right now and is about to be discarded. There is nothing to reuse in the number 3, but the buffer owned by a temporary &lt;code&gt;std::string&lt;/code&gt; is worth stealing, and that is exactly what move constructors do with xvalues.&lt;/p&gt;
&lt;h2 id=&quot;asking-decltype&quot;&gt;Asking decltype&lt;a class=&quot;anchor&quot; href=&quot;#asking-decltype&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Applied to an expression of type &lt;code&gt;T&lt;/code&gt;, &lt;code&gt;decltype&lt;/code&gt; encodes the category in the type it returns:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Category&lt;/th&gt;&lt;th&gt;&lt;code&gt;decltype&lt;/code&gt; yields&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;lvalue&lt;/td&gt;&lt;td&gt;&lt;code&gt;T&amp;amp;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;xvalue&lt;/td&gt;&lt;td&gt;&lt;code&gt;T&amp;amp;&amp;amp;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;prvalue&lt;/td&gt;&lt;td&gt;&lt;code&gt;T&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;There is one exception. For an unparenthesized name, &lt;code&gt;decltype&lt;/code&gt; reports the declared type of the entity instead. Given &lt;code&gt;int x = 3;&lt;/code&gt;, &lt;code&gt;decltype(x)&lt;/code&gt; is &lt;code&gt;int&lt;/code&gt;, even though &lt;code&gt;x&lt;/code&gt; is an lvalue. &lt;a rel=&quot;external&quot; href=&quot;https://en.cppreference.com/cpp/language/decltype&quot;&gt;cppreference&lt;/a&gt; states the rule plainly:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If the argument is an unparenthesized id-expression or an unparenthesized class member access expression, then decltype yields the type of the entity named by this expression.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Adding parentheses turns the name into an ordinary expression, so &lt;code&gt;decltype((x))&lt;/code&gt; is &lt;code&gt;int&amp;amp;&lt;/code&gt;. That is why the macros wrap their argument twice. Here they are in action; every assertion holds with GCC and Clang in C++17 mode:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;utility&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;z-l-6 z-d-6&quot;&gt; S&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; m&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    static_assert&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;IS_LVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    static_assert&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;IS_LVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;             //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; string literals are lvalues&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    static_assert&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;IS_PRVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    static_assert&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;IS_PRVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    static_assert&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;IS_PRVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;hi&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    static_assert&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;IS_XVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    static_assert&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;IS_XVALUE&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;S&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;               //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; via temporary materialization&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;what-std-move-actually-does&quot;&gt;What std::move actually does&lt;a class=&quot;anchor&quot; href=&quot;#what-std-move-actually-does&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;Despite its name, &lt;code&gt;std::move&lt;/code&gt; moves nothing. It is a cast: &lt;code&gt;std::move(t)&lt;/code&gt; is exactly &lt;code&gt;static_cast&amp;lt;std::remove_reference_t&amp;lt;T&amp;gt;&amp;amp;&amp;amp;&amp;gt;(t)&lt;/code&gt;, an xvalue that refers to &lt;code&gt;t&lt;/code&gt;. Its only effect is to change the category of an expression, which in turn changes which overload is chosen. When the chosen overload is a move constructor or move assignment operator, &lt;em&gt;that&lt;/em&gt; function does the moving.&lt;/p&gt;
&lt;p&gt;Read &lt;code&gt;std::move(x)&lt;/code&gt; as a promise rather than an action: “I am done with &lt;code&gt;x&lt;/code&gt;; take whatever you can use.”&lt;/p&gt;
&lt;p&gt;Because it is only a cast, &lt;code&gt;std::move&lt;/code&gt; can quietly do nothing useful. Applied to a &lt;code&gt;const&lt;/code&gt; object it produces a &lt;code&gt;const T&amp;amp;&amp;amp;&lt;/code&gt;, which cannot bind to the move constructor’s &lt;code&gt;T&amp;amp;&amp;amp;&lt;/code&gt; but binds happily to the copy constructor’s &lt;code&gt;const T&amp;amp;&lt;/code&gt;. The code compiles, and it copies:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;const&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; a&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; make_string&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; b&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; std&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;move&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;   //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; copies: a is const&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Clang-Tidy’s &lt;code&gt;performance-move-const-arg&lt;/code&gt; check flags this pattern.&lt;/p&gt;
&lt;h2 id=&quot;a-c-23-twist&quot;&gt;A C++23 twist&lt;a class=&quot;anchor&quot; href=&quot;#a-c-23-twist&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;C++23 changed the category of one familiar expression. Under &lt;a rel=&quot;external&quot; href=&quot;https://wg21.link/p2266&quot;&gt;P2266, “Simpler implicit move”&lt;/a&gt;, a local variable or parameter named in a &lt;code&gt;return&lt;/code&gt; statement is an xvalue rather than an lvalue. Earlier standards already &lt;em&gt;moved&lt;/em&gt; from such variables when returning by value; C++23 goes further and changes the category itself. So this now compiles:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; forward_it&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; return&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; }&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;   //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; C++20: error, x is an lvalue&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;because &lt;code&gt;x&lt;/code&gt; in the return statement is an xvalue, and an rvalue reference can bind to it.&lt;/p&gt;
&lt;p&gt;It also changes what &lt;code&gt;decltype(auto)&lt;/code&gt; deduces, since the parenthesized &lt;code&gt;(x)&lt;/code&gt; in a return statement is now an xvalue too:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;decltype&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;auto&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; g&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; {&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt; return&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; C++20: g() returns int&amp;amp;   (an lvalue reference to a dead local)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; C++23: g() returns int&amp;amp;&amp;amp;  (an rvalue reference to a dead local)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Both versions return a dangling reference, which is the real lesson: never write &lt;code&gt;return (x);&lt;/code&gt; in a function declared &lt;code&gt;decltype(auto)&lt;/code&gt;. GCC and Clang both implement the C++23 rule in &lt;code&gt;-std=c++23&lt;/code&gt; mode.&lt;/p&gt;
&lt;h2 id=&quot;in-one-table&quot;&gt;In one table&lt;a class=&quot;anchor&quot; href=&quot;#in-one-table&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Category&lt;/th&gt;&lt;th style=&quot;text-align: center&quot;&gt;Has identity&lt;/th&gt;&lt;th style=&quot;text-align: center&quot;&gt;Resources may be taken&lt;/th&gt;&lt;th&gt;Typical examples&lt;/th&gt;&lt;th&gt;&lt;code&gt;decltype((e))&lt;/code&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;lvalue&lt;/td&gt;&lt;td style=&quot;text-align: center&quot;&gt;yes&lt;/td&gt;&lt;td style=&quot;text-align: center&quot;&gt;no&lt;/td&gt;&lt;td&gt;&lt;code&gt;x&lt;/code&gt;, &lt;code&gt;*p&lt;/code&gt;, &lt;code&gt;a[i]&lt;/code&gt;, &lt;code&gt;&quot;hi&quot;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;T&amp;amp;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;xvalue&lt;/td&gt;&lt;td style=&quot;text-align: center&quot;&gt;yes&lt;/td&gt;&lt;td style=&quot;text-align: center&quot;&gt;yes&lt;/td&gt;&lt;td&gt;&lt;code&gt;std::move(x)&lt;/code&gt;, &lt;code&gt;S().m&lt;/code&gt;, a local in &lt;code&gt;return x;&lt;/code&gt; (C++23)&lt;/td&gt;&lt;td&gt;&lt;code&gt;T&amp;amp;&amp;amp;&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;prvalue&lt;/td&gt;&lt;td style=&quot;text-align: center&quot;&gt;no&lt;/td&gt;&lt;td style=&quot;text-align: center&quot;&gt;yes&lt;/td&gt;&lt;td&gt;&lt;code&gt;3&lt;/code&gt;, &lt;code&gt;x + 1&lt;/code&gt;, &lt;code&gt;S()&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;T&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
</description>
      </item>
      <item>
          <title>Setting a Default C++ Standard for clangd</title>
          <pubDate>Sun, 07 Aug 2022 00:00:00 +0000</pubDate>
          <author>Jia Zhang</author>
          <link>https://jiaz.dev/blog/clangd-cpp-standard/</link>
          <guid>https://jiaz.dev/blog/clangd-cpp-standard/</guid>
          <description xml:base="https://jiaz.dev/blog/clangd-cpp-standard/">&lt;p&gt;clangd needs to know which language standard a file is written in. Inside a real project it reads the answer from &lt;code&gt;compile_commands.json&lt;/code&gt;. For everything else, such as a scratch file, a snippet from a blog post or a header opened on its own, it falls back to the compiler’s default. Clang 15 and earlier defaulted to &lt;code&gt;gnu++14&lt;/code&gt;, so &lt;code&gt;std::string_view&lt;/code&gt; showed up as an error. Since Clang 16 the default is &lt;code&gt;gnu++17&lt;/code&gt;, which still rules out concepts, ranges, &lt;code&gt;std::format&lt;/code&gt; and everything else after C++17.&lt;/p&gt;
&lt;p&gt;There are two ways to change that, and they behave differently in one important respect.&lt;/p&gt;
&lt;h2 id=&quot;the-fallback-flags-recommended&quot;&gt;The fallback flags (recommended)&lt;a class=&quot;anchor&quot; href=&quot;#the-fallback-flags-recommended&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;clangd accepts &lt;em&gt;fallback flags&lt;/em&gt;: extra arguments used only when a file has no entry in a compilation database. In VS Code they are a setting of the clangd extension:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;json&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;clangd.fallbackFlags&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;: &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;-std=c++23&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Other editors pass the same list as the &lt;code&gt;fallbackFlags&lt;/code&gt; initialization option of the language server.&lt;/p&gt;
&lt;p&gt;Files that belong to a project are unaffected and keep the standard their build uses. The catch is that the flags apply to every loose file, C files included, and clangd will complain that &lt;code&gt;-std=c++23&lt;/code&gt; is not allowed with C. If you rarely open stray C files, this is the setting to use.&lt;/p&gt;
&lt;h2 id=&quot;the-user-config-file&quot;&gt;The user config file&lt;a class=&quot;anchor&quot; href=&quot;#the-user-config-file&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;clangd also reads a user-level configuration file:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Platform&lt;/th&gt;&lt;th&gt;Path&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;Linux&lt;/td&gt;&lt;td&gt;&lt;code&gt;~/.config/clangd/config.yaml&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;macOS&lt;/td&gt;&lt;td&gt;&lt;code&gt;~/Library/Preferences/clangd/config.yaml&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Windows&lt;/td&gt;&lt;td&gt;&lt;code&gt;%LocalAppData%\clangd\config.yaml&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;This file can be conditioned on the path, so it can keep C files out:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;  P&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;athExclude&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;*\.c&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt; .&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;*\.h&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;ompileFlags&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;  A&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;dd&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;-s&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;td=c++23&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Excluding &lt;code&gt;.h&lt;/code&gt; keeps C headers clean, at a price: C++ headers that use the &lt;code&gt;.h&lt;/code&gt; extension fall back to the compiler default. If your C++ code uses &lt;code&gt;.h&lt;/code&gt; headers, exclude only &lt;code&gt;.*\.c&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Be aware of what &lt;code&gt;Add&lt;/code&gt; does, though. It appends the flag to &lt;em&gt;every&lt;/em&gt; compile command, including those that come from &lt;code&gt;compile_commands.json&lt;/code&gt;, and when a command contains two &lt;code&gt;-std&lt;/code&gt; flags the last one wins. A project that builds as C++17 will therefore be analysed as C++23, and clangd may accept code that your compiler rejects. A project-level &lt;code&gt;.clangd&lt;/code&gt; file cannot undo this by adding its own &lt;code&gt;-std&lt;/code&gt;, because the user configuration is applied after it. The safe use of this file is a rule scoped to the directories where you keep loose code:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;yaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;I&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;  P&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;athMatch&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt; /&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;home/me/scratch/.*\.(cpp|cc|hpp)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;ompileFlags&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;  A&lt;/span&gt;&lt;span class=&quot;z-l-13 z-d-14&quot;&gt;dd&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; [&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;-s&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;td=c++23&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;https://clangd.llvm.org/config&quot;&gt;configuration reference&lt;/a&gt; documents the full format.&lt;/p&gt;
&lt;h2 id=&quot;checking-the-result&quot;&gt;Checking the result&lt;a class=&quot;anchor&quot; href=&quot;#checking-the-result&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;clangd --check=path/to/file.cpp&lt;/code&gt; prints the compile command it settled on, either “Compile command from CDB is” or “Generic fallback command is”, followed by the arguments. Look for the &lt;code&gt;-std&lt;/code&gt; flag at the end of the line. Note that &lt;code&gt;--check&lt;/code&gt; does not see editor settings, so it reflects the config file but not &lt;code&gt;clangd.fallbackFlags&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Every behaviour described here was checked with clangd 23.&lt;/em&gt;&lt;/p&gt;
</description>
      </item>
      <item>
          <title>Placing a Million Queens in a Tenth of a Second</title>
          <pubDate>Sun, 27 Dec 2020 00:00:00 +0000</pubDate>
          <author>Jia Zhang</author>
          <link>https://jiaz.dev/blog/n-queens/</link>
          <guid>https://jiaz.dev/blog/n-queens/</guid>
          <description xml:base="https://jiaz.dev/blog/n-queens/">&lt;p&gt;The N-queens puzzle asks you to place N queens on an N × N board so that no two attack each other. Counting &lt;em&gt;every&lt;/em&gt; solution is hard. The number grows faster than exponentially: Michael Simkin &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/2107.13460&quot;&gt;showed in 2021&lt;/a&gt; that it is roughly (0.143 N)&lt;sup&gt;N&lt;/sup&gt;, and exact counts are known only for small boards. Finding &lt;em&gt;one&lt;/em&gt; solution is a different problem entirely. With the right search, a desktop machine finds one for a million queens in well under a second.&lt;/p&gt;
&lt;p&gt;This post describes such a search and gives a complete C program for it. If the puzzle is new to you, the &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Eight_queens_puzzle&quot;&gt;Wikipedia article&lt;/a&gt; is a good introduction.&lt;/p&gt;
&lt;h2 id=&quot;a-small-example&quot;&gt;A small example&lt;a class=&quot;anchor&quot; href=&quot;#a-small-example&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;For N = 4 there are exactly two solutions (&lt;code&gt;1&lt;/code&gt; marks a queen):&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0100        0010&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0001        1000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1000        0001&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;0010        0100&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Backtracking finds these instantly, but plain backtracking slows to a crawl long before N reaches the thousands. Local search does not explore the board systematically. It starts from a nearly correct placement and repairs it.&lt;/p&gt;
&lt;p&gt;One caveat before going further. If all you need is &lt;em&gt;a&lt;/em&gt; solution, you do not need search at all: &lt;a rel=&quot;external&quot; href=&quot;https://en.wikipedia.org/wiki/Eight_queens_puzzle#Existence_of_solutions&quot;&gt;explicit constructions&lt;/a&gt; produce one for every N ≥ 4 in linear time, and they are far faster than anything in this post (see &lt;a href=&quot;https://jiaz.dev/blog/n-queens/#how-it-compares-with-a-formula&quot;&gt;the comparison&lt;/a&gt; at the end). Local search is still worth studying, because the same ideas solve constraint problems that have no closed form, from scheduling to SAT, and the N-queens puzzle is the cleanest place to see them work.&lt;/p&gt;
&lt;h2 id=&quot;the-algorithm&quot;&gt;The algorithm&lt;a class=&quot;anchor&quot; href=&quot;#the-algorithm&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Representation.&lt;/strong&gt; Every row holds exactly one queen, so a placement is just an array: &lt;code&gt;queen[i] = j&lt;/code&gt; means the queen in row &lt;code&gt;i&lt;/code&gt; stands in column &lt;code&gt;j&lt;/code&gt;. If &lt;code&gt;queen&lt;/code&gt; is a permutation of &lt;code&gt;1..N&lt;/code&gt;, no two queens share a row or a column. The only conflicts left are diagonal ones.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Counting conflicts.&lt;/strong&gt; Number the anti-diagonals by &lt;code&gt;row + column&lt;/code&gt; (2 to 2N) and the diagonals by &lt;code&gt;column − row&lt;/code&gt; (1 − N to N − 1). Two arrays, &lt;code&gt;dn&lt;/code&gt; and &lt;code&gt;dp&lt;/code&gt;, count the queens on each. The cost function &lt;code&gt;collisions&lt;/code&gt; adds up, over all diagonals, the queens beyond the first. A placement is a solution exactly when &lt;code&gt;collisions&lt;/code&gt; is zero.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Moves.&lt;/strong&gt; The only move is to swap the columns of two rows. A swap keeps &lt;code&gt;queen&lt;/code&gt; a permutation and touches just four diagonals, so its effect on &lt;code&gt;collisions&lt;/code&gt; costs constant time to compute.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A good start.&lt;/strong&gt; Most of the speed comes from the initial placement. Row by row, the program picks a random column among those not yet used and keeps it only if it attacks none of the queens already placed. After 4N attempts it stops being picky and places the remaining queens at random. For large N this greedy start leaves only a handful of conflicts, so the search that follows is short.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hill climbing.&lt;/strong&gt; The program keeps a list of the queens under attack. For each one it picks a random partner and swaps the two if that lowers &lt;code&gt;collisions&lt;/code&gt;. Rebuilding the list after every swap would dominate the running time, so it is rebuilt only when &lt;code&gt;collisions&lt;/code&gt; falls below half its value at the previous rebuild.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Restarts.&lt;/strong&gt; If about 32N attempts pass without reaching zero, the search is stuck in a local minimum, and the program starts over from a fresh random placement.&lt;/p&gt;
&lt;p&gt;The method is essentially the conflict-minimizing local search of Rok Sosič and Jun Gu, described in &lt;a rel=&quot;external&quot; href=&quot;https://doi.org/10.1109/69.317698&quot;&gt;&lt;em&gt;Efficient Local Search with Conflict Minimization: A Case Study of the n-Queens Problem&lt;/em&gt;&lt;/a&gt; (IEEE TKDE, 1994).&lt;/p&gt;
&lt;h2 id=&quot;the-program&quot;&gt;The program&lt;a class=&quot;anchor&quot; href=&quot;#the-program&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The program takes N on the command line and, optionally, a file to write the solution to: the column of each row, followed by a drawing of the board when N ≤ 20. It checks the solution independently before reporting success.&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;stdint.h&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;stdio.h&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;stdlib.h&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;time.h&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; queen[i] = j: the queen in row i stands in column j (both numbered from 1).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; dn[k]: number of queens on the anti-diagonal k = row + column, k in [2, 2n].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; dp[k]: number of queens on the diagonal k = column - row, k in [1-n, n-1].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; attack[]: rows whose queen is currently under attack.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;dn&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;dp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;attack&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; long&lt;/span&gt;&lt;span&gt; collisions&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; queens beyond the first on each diagonal, summed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; uint64_t&lt;/span&gt;&lt;span&gt; rng_state&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; 64-bit linear congruential generator (Knuth&amp;#39;s MMIX constants);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; only the high 32 bits are used.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; uint32_t&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; next_random&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    rng_state &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; rng_state &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 6364136223846793005&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;ULL&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1442695040888963407&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;ULL&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;uint32_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;rng_state &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 32&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; A random integer in [0, k).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; random_below&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;next_random&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; %&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;uint32_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; swap_int&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;q&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    int&lt;/span&gt;&lt;span&gt; temp &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    *&lt;/span&gt;&lt;span&gt;p &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;q&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    *&lt;/span&gt;&lt;span&gt;q &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; temp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Take the queen in `row` off the board.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; remove_queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    int&lt;/span&gt;&lt;span&gt; col &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;dn&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;col &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; the diagonal still holds another queen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        --&lt;/span&gt;&lt;span&gt;collisions&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;col &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        --&lt;/span&gt;&lt;span&gt;collisions&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Put the queen of `row` in column `col`.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; place_queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; col&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;    queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span&gt; col&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;dn&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;col &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; the diagonal was already occupied&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        ++&lt;/span&gt;&lt;span&gt;collisions&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;dp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;col &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        ++&lt;/span&gt;&lt;span&gt;collisions&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Swap the queens of rows i and j, keeping dn, dp and collisions exact.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; swap_queens&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    int&lt;/span&gt;&lt;span&gt; ci &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; cj &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    remove_queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    remove_queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    place_queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; cj&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    place_queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; ci&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; is_attacked&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; dn&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; +&lt;/span&gt;&lt;span&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ||&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; dp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; -&lt;/span&gt;&lt;span&gt; row&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Rebuild attack[] and return the number of queens under attack.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; compute_attack&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    int&lt;/span&gt;&lt;span&gt; k &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;is_attacked&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;            attack&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;++&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    return&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Build the initial placement.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; initialize&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; k &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; k &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;        dn&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; k &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; -&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; k &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;        dp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    collisions &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;        queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Start as close to a solution as possible, so the climb is short:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; fill rows in order, each time trying a random unused column and&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; keeping it only if it conflicts with nothing placed so far.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; j is the next row to fill; at most 4n attempts are made.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    long&lt;/span&gt;&lt;span&gt; search_max &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    int&lt;/span&gt;&lt;span&gt; j &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;long&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; search_max &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; j &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        int&lt;/span&gt;&lt;span&gt; m &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; j &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; random_below&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span&gt; j &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; random m in [j, n]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;dn&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; +&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; dp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; -&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ==&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;            swap_int&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;            place_queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;            ++&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Shuffle the remaining columns into the remaining rows.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        int&lt;/span&gt;&lt;span&gt; m &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; random_below&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;        swap_int&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;m&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;        place_queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Find one solution: initialize, hill-climb, restart when stuck.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; void&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; solve&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;        initialize&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;collisions &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Rebuilding attack[] after every swap is too expensive;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; rebuild it only once collisions has fallen below `limit`.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        long&lt;/span&gt;&lt;span&gt; limit &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; collisions &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        int&lt;/span&gt;&lt;span&gt; number_of_attacks &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; compute_attack&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; A large loopcount means the climb is stuck in a local minimum.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        long&lt;/span&gt;&lt;span&gt; loopcount &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        while&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;loopcount &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 32&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;            for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; k &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; k &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; number_of_attacks&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;                //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Try swapping an attacked queen with a random one.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;                int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; attack&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;                int&lt;/span&gt;&lt;span&gt; j &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; +&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; random_below&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;==&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                    continue&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;                long&lt;/span&gt;&lt;span&gt; before &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; collisions&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;                swap_queens&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;collisions &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span&gt; before&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;                {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;                    swap_queens&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; no improvement: undo&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                    continue&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;collisions &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                    return&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;collisions &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; limit&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;                {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;                    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; The board has changed enough to refresh the list.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    limit &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; collisions &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    number_of_attacks &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; compute_attack&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;                }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            loopcount &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+=&lt;/span&gt;&lt;span&gt; number_of_attacks&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Check the answer from scratch: one queen per column, at most one per diagonal.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;static&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; int&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; verify&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;col &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; calloc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;d1 &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; calloc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    char&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;d2 &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; calloc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    int&lt;/span&gt;&lt;span&gt; ok &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; col &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; d1 &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; d2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; ok &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;        int&lt;/span&gt;&lt;span&gt; c &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;c &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ||&lt;/span&gt;&lt;span&gt; c &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; col&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ||&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; d1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;c &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ||&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; d2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;c &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ok &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        else&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;            col&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; d1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;c &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; d2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;c &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; =&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    free&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;col&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    free&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;d1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    free&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;d2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    return&lt;/span&gt;&lt;span&gt; ok&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; main&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; argc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; char&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;argc &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ||&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; atoi&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ||&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 100000000&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;usage: &lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt; N [output-file]   (1 &amp;lt;= N &amp;lt;= 100000000)&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; argv&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ||&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;        printf&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;no solution for N = &lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; Rows and columns are numbered from 1, so queen, attack and dn simply&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; leave their first slots unused. dp is indexed from 1-n to n-1, so it&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; points into the middle of its buffer: dp[1-n] is pre_dp[0].&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;    //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; (Pointing *before* a buffer would be undefined behaviour in C.)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    queen &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; malloc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    attack &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; malloc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    dn &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; malloc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    int&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span&gt;pre_dp &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; malloc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; *&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;size_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;!&lt;/span&gt;&lt;span&gt;queen &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; !&lt;/span&gt;&lt;span&gt;attack &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; !&lt;/span&gt;&lt;span&gt;dn &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; !&lt;/span&gt;&lt;span&gt;pre_dp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;        fprintf&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;stderr&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;out of memory&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        return&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    dp &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span&gt; pre_dp &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    rng_state &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;uint64_t&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    clock_t&lt;/span&gt;&lt;span&gt; start &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; clock&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    solve&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;    clock_t&lt;/span&gt;&lt;span&gt; end &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; clock&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    printf&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;N = &lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;: found a solution in &lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;%.3f&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt; seconds (&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;           (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;double&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;end &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;-&lt;/span&gt;&lt;span&gt; start&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; /&lt;/span&gt;&lt;span&gt; CLOCKS_PER_SEC&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; verify&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;verified&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;INVALID&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;argc &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        FILE &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;*&lt;/span&gt;&lt;span&gt;fp &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt; fopen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;!&lt;/span&gt;&lt;span&gt;fp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;        {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;            perror&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;            return&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; The i-th number is the column of the queen in row i; for N &amp;lt;= 20,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt;        //&lt;/span&gt;&lt;span class=&quot;z-l-16 z-d-9&quot;&gt; also draw the board (* is a queen).&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;            fprintf&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;fp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;%d&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;%c&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;==&lt;/span&gt;&lt;span&gt; n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;        if&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span&gt;n &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 20&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;            for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;            {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;                for&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt; (&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;int&lt;/span&gt;&lt;span&gt; j &lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span&gt; j &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ++&lt;/span&gt;&lt;span&gt;j&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;                    fputc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;j &lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;z-l-21 z-d-16&quot;&gt; queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; ?&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-18 z-d-21&quot;&gt; :&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt; &amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-23 z-d-18&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; fp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;                fputc&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-17 z-d-20&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;z-l-24 z-d-19&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;,&lt;/span&gt;&lt;span&gt; fp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;        fclose&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;fp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    free&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;queen&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    free&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;attack&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    free&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;dn&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-10 z-d-11&quot;&gt;    free&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;(&lt;/span&gt;&lt;span&gt;pre_dp&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-3 z-d-4&quot;&gt;    return&lt;/span&gt;&lt;span class=&quot;z-l-7 z-d-3&quot;&gt; 0&lt;/span&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span class=&quot;z-l-15 z-d-7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Build and run it:&lt;/p&gt;
&lt;pre class=&quot;giallo z-l-code z-d-code&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ cc -O2 -o nqueens nqueens.c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ ./nqueens 8 out.txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;N = 8: found a solution in 0.000 seconds (verified)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ cat out.txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;5 1 8 6 3 7 2 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;----*---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;*-------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-------*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-----*--&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;--*-----&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;------*-&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-*------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;---*----&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ ./nqueens 1000000&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;N = 1000000: found a solution in 0.060 seconds (verified)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The seed comes from the clock, so each run usually prints a different solution. The timings were measured on an AMD Ryzen 9 9950X. The running time is not linear in N. Going from 100,000 to 1,000,000 queens multiplies it by about 14 (4 ms to 58 ms), and going on to 10,000,000 multiplies it by about 32 (to 1.8 s). The algorithm is not to blame. Hardware counters (&lt;code&gt;perf stat&lt;/code&gt;, whole program including the final check) show the instruction count growing almost exactly in proportion to N, 21 million at N = 100,000 and 2.0 billion at N = 10,000,000, while the cycle count grows from 21 million to 10.4 billion. Instructions per cycle fall from about 1.0 to 0.19. Every swap touches rows and diagonals at random, and at ten million queens the four arrays occupy about 240 MB, far more than any cache, so the processor spends most of its time waiting for memory. Making the solver faster for very large N is a data-layout problem, not a search problem.&lt;/p&gt;
&lt;h2 id=&quot;how-it-compares-with-a-formula&quot;&gt;How it compares with a formula&lt;a class=&quot;anchor&quot; href=&quot;#how-it-compares-with-a-formula&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;The simplest explicit construction lists the even columns, then the odd ones, with a small adjustment when N mod 6 is 2 or 3. I implemented it, checked it with the same verifier for every N from 4 to 3,000, and timed it against the search on the same machine:&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th style=&quot;text-align: right&quot;&gt;N&lt;/th&gt;&lt;th style=&quot;text-align: right&quot;&gt;Local search&lt;/th&gt;&lt;th style=&quot;text-align: right&quot;&gt;Explicit construction&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: right&quot;&gt;1,000,000&lt;/td&gt;&lt;td style=&quot;text-align: right&quot;&gt;0.056 s&lt;/td&gt;&lt;td style=&quot;text-align: right&quot;&gt;0.001 s&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: right&quot;&gt;10,000,000&lt;/td&gt;&lt;td style=&quot;text-align: right&quot;&gt;1.9 s&lt;/td&gt;&lt;td style=&quot;text-align: right&quot;&gt;0.007–0.018 s&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: right&quot;&gt;100,000,000&lt;/td&gt;&lt;td style=&quot;text-align: right&quot;&gt;25 s&lt;/td&gt;&lt;td style=&quot;text-align: right&quot;&gt;0.07–0.18 s&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;The construction wins by two orders of magnitude, and the gap widens with N. It writes each entry of &lt;code&gt;queen&lt;/code&gt; exactly once, in order, so it runs at the speed of sequential memory writes, while the search makes random accesses that miss the cache. (The construction’s timings vary by N mod 6 and from run to run, probably because of first-touch page faults.) Use the formula when you need a solution; use the search when you need to understand local search.&lt;/p&gt;
&lt;h2 id=&quot;what-the-first-version-got-wrong&quot;&gt;What the first version got wrong&lt;a class=&quot;anchor&quot; href=&quot;#what-the-first-version-got-wrong&quot; aria-label=&quot;Link to this section&quot;&gt;#&lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;I wrote the first version of this program in 2020. Revisiting it in 2026 turned up three bugs worth recording, because each one is easy to make:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;An overflowing random index.&lt;/strong&gt; The partner for a swap came from &lt;code&gt;((rand() &amp;lt;&amp;lt; 6) + rand()) % n + 1&lt;/code&gt;. With glibc, &lt;code&gt;RAND_MAX&lt;/code&gt; is 2&lt;sup&gt;31&lt;/sup&gt; − 1, so the shift overflows &lt;code&gt;int&lt;/code&gt;. That is undefined behaviour, and in practice the index went negative and the program wrote outside its arrays. For large N it printed “solutions” that were not even permutations. The program now carries its own 64-bit generator.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;An inexact shortcut.&lt;/strong&gt; The change in &lt;code&gt;collisions&lt;/code&gt; caused by a swap was computed with a closed-form formula that is wrong when the two queens share a diagonal, so the running count drifted away from the truth. A swap is now applied as “remove both queens, put them back exchanged”, and undone if it does not help. That costs a few more memory accesses and is always exact.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing edge cases.&lt;/strong&gt; The arrays were sized for 123,456,789 queens regardless of N, and N = 2 or N = 3, which have no solution, looped forever.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The program also verifies its answer independently now. Had the original done that, the first bug would have surfaced on the first large run.&lt;/p&gt;
</description>
      </item>
    </channel>
</rss>
