<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://www.baszerr.eu/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel xmlns:g="http://base.google.com/ns/1.0">
        <title>BaSzErr - blog:2013:12:02</title>
        <description></description>
        <link>https://www.baszerr.eu/</link>
        <lastBuildDate>Wed, 06 May 2026 09:38:05 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://www.baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://www.baszerr.eu/</link>
        </image>
        <item>
            <title>got_a_change</title>
            <link>https://www.baszerr.eu/doku.php?id=blog:2013:12:02:got_a_change</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;got_a_change&quot;&gt;2013.12.02 - got a change?&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
about two weeks ago a friend of mine sent me a link to &lt;a href=&quot;http://bepartofsomething.eu&quot; class=&quot;urlextern&quot; title=&quot;http://bepartofsomething.eu&quot; rel=&quot;ugc nofollow&quot;&gt;a page with a problem-solving quest&lt;/a&gt;. of course this was a propaganda page. ;) anyway i was asked to provide a correct answer… since the deadline for submitting answers is officially over, let&amp;#039;s have a look on what the solution looked like.
&lt;/p&gt;

&lt;p&gt;
the task was to tell how many ways one can change 1024 into coins of 1,2,4,8,16,32,64,128,256,512. this is very famous problem of discrete mathematics – googling for an answer gives examples along with the source codes in any languages of your choice. anyway, not knowing “the best way” in advance i&amp;#039;ve figured out this will be a nice day to play…
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;2013.12.02 - got a change?&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;got_a_change&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:1,&amp;quot;range&amp;quot;:&amp;quot;1-725&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit2&quot; id=&quot;first_approach&quot;&gt;first approach&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
after thinking a bit, the most obvious answer was to find number of all possible solutions to solve the equation of the form:
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;A*512 + B*256 + ... + I*2 + J*1 == 1024&lt;/pre&gt;

&lt;p&gt;
looks like simple math. since A..J are non-negative numbers, and sum must be 1024, one can derive maximum values of each of parameters. simple. lets make it fun then!
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;first approach&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;first_approach&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:2,&amp;quot;range&amp;quot;:&amp;quot;726-1090&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit3&quot; id=&quot;prolog&quot;&gt;prolog&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
&lt;a href=&quot;https://en.wikipedia.org/wiki/gprolog&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/gprolog&quot;&gt;gprolog&lt;/a&gt; has a &lt;a href=&quot;https://en.wikipedia.org/wiki/constraint satisfaction problem&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/constraint satisfaction problem&quot;&gt;CSP&lt;/a&gt; module for problems over &lt;a href=&quot;https://en.wikipedia.org/wiki/finite set&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/finite set&quot;&gt;finite domains&lt;/a&gt; (FD). so we end up with something like this (&lt;a href=&quot;https://www.baszerr.eu/lib/exe/fetch.php?media=blog:2013:12:02:kb.pl&quot; class=&quot;media mediafile mf_pl&quot; title=&quot;blog:2013:12:02:kb.pl (494 B)&quot;&gt;download&lt;/a&gt;):
&lt;/p&gt;
&lt;pre class=&quot;code prolog&quot;&gt;change&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;A&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;B&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;C&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;D&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;E&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;F&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;G&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;H&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;I&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;J R&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;:-&lt;/span&gt;
  LD &lt;span class=&quot;sy6&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;A&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;B&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;C&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;D&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;E&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;F&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;G&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;H&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;I&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;J&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;
  fd_domain&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;LD&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;
  A&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;512&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; B&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;256&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; C&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;128&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; D&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;64&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; E&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; F&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; G&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; H&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; I&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; J&lt;span class=&quot;sy3&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt; #&lt;span class=&quot;sy6&quot;&gt;=&lt;/span&gt; R&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;
  fd_labeling&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;LD&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span class=&quot;sy4&quot;&gt;.&lt;/span&gt;
&amp;nbsp;
len&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;.&lt;/span&gt;
len&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;_&lt;span class=&quot;sy5&quot;&gt;|&lt;/span&gt;T&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; L&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;:-&lt;/span&gt;
  len&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;T&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; Lt&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;
  L #&lt;span class=&quot;sy6&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;sy3&quot;&gt;+&lt;/span&gt; Lt
  &lt;span class=&quot;sy4&quot;&gt;.&lt;/span&gt;
&amp;nbsp;
q&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;N&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; OUT&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;:-&lt;/span&gt;
  &lt;a href=&quot;http://pauillac.inria.fr/~deransar/prolog/bips.html&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;findall&lt;/span&gt;&lt;/a&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;A&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;B&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;C&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;D&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;E&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;F&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;G&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;H&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;I&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;J&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; change&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;A&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;B&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;C&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;D&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;E&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;F&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;G&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;H&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;I&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;J&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; N&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; LST&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt;
  len&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;LST&lt;span class=&quot;sy4&quot;&gt;,&lt;/span&gt; OUT&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span class=&quot;sy4&quot;&gt;.&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
nice, simple… but uses a bit too much memory. the problem is number of possible solutions. unfortunately prolog did not noticed it can generate lazy list and just count its length on a fly. any way it was generating about 22k answer per second, which is not mind-blowing-fast. the good news is that we have working reference implementation for finding number of changes for smaller numbers.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;prolog&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;prolog&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:0,&amp;quot;secid&amp;quot;:3,&amp;quot;range&amp;quot;:&amp;quot;1091-2086&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit4&quot; id=&quot;na_t_ive_code&quot;&gt;na(t)ive code&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
as a next step i decided to try luck with compiled languages. i&amp;#039;ve created a dead-simple implementation in &lt;a href=&quot;https://en.wikipedia.org/wiki/C++&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/C++&quot;&gt;C++&lt;/a&gt; (&lt;a href=&quot;https://www.baszerr.eu/lib/exe/fetch.php?media=blog:2013:12:02:naive.cpp&quot; class=&quot;media mediafile mf_cpp&quot; title=&quot;blog:2013:12:02:naive.cpp (1.7 KB)&quot;&gt;download&lt;/a&gt;):
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;&lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt; Number&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; main&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;long&lt;/span&gt; count &lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;co1&quot;&gt;// b*512 + ... + k*1 = 1024,&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Number b&lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; b&lt;span class=&quot;sy1&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;++&lt;/span&gt;b&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Number c&lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; c&lt;span class=&quot;sy1&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;++&lt;/span&gt;c&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
      &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp;
      &lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
                      &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Number k&lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; k&lt;span class=&quot;sy1&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;++&lt;/span&gt;k&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
                      &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
                        &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; b&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;512&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; c&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;256&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; d&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;128&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; e&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;64&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; f&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; g&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; h&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; i&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; j&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; k&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1024&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
                        &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
                          &lt;span class=&quot;sy2&quot;&gt;++&lt;/span&gt;count&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
                          &lt;span class=&quot;kw3&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; b &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*512  + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; c &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*256  + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; d &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*128  + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; e &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*64   + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; f &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*32   + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; g &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*16   + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; h &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*8    + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; i &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*4    + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; j &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*2    + &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; k &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;*1      &amp;quot;&lt;/span&gt;
                               &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
                      &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
      &lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
      &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
    &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;kw3&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; count &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
ok - got 122k answers per second. way faster… but still quite a bit of solutions to search.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;na(t)ive code&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;na_t_ive_code&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:1,&amp;quot;secid&amp;quot;:4,&amp;quot;range&amp;quot;:&amp;quot;2087-3483&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit5&quot; id=&quot;na_t_ive_code_mkii&quot;&gt;na(t)ive code mk.II&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
next approach was to use a git of domain knowledge again. note that most of the combinations will sum to over 1024 on the outer loops. say, if &lt;em&gt;b==2&lt;/em&gt; in the above example, there can be only one solution and there is no point in scanning all possible values of remaining variables. this cuts search space in 3. applying similar thinking to other variables cuts space more and more. so the improved implementation looks like this (&lt;a href=&quot;https://www.baszerr.eu/lib/exe/fetch.php?media=blog:2013:12:02:naive2.cpp&quot; class=&quot;media mediafile mf_cpp&quot; title=&quot;blog:2013:12:02:naive2.cpp (2.4 KB)&quot;&gt;download&lt;/a&gt;):
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;&lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; main&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw2&quot;&gt;constexpr&lt;/span&gt; Number value &lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;long&lt;/span&gt; count &lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;co1&quot;&gt;// b*512 + ... + k*1 = 1024,&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Number b&lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; b&lt;span class=&quot;sy1&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;++&lt;/span&gt;b&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;auto&lt;/span&gt; pb &lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt; b&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; pb &lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt; value &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
      &lt;span class=&quot;kw1&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
                    &lt;span class=&quot;kw1&quot;&gt;for&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;Number k&lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; k&lt;span class=&quot;sy1&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;++&lt;/span&gt;k&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
                    &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
                      &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;auto&lt;/span&gt; pk &lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt; pj &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; k&lt;span class=&quot;sy2&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
                      &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; pk &lt;span class=&quot;sy1&quot;&gt;==&lt;/span&gt; value &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
                      &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
                        &lt;span class=&quot;sy2&quot;&gt;++&lt;/span&gt;count&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
                        &lt;span class=&quot;kw1&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
                      &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
                    &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
      &lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
      &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
    &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;kw3&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; value &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot; -&amp;gt; &amp;quot;&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; count &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
this one gets solution in 4.5 minutes on my computer. ok – so we finally have a working version, providing the answer (!=42, btw ;)).
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;na(t)ive code mk.II&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;na_t_ive_code_mkii&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:2,&amp;quot;secid&amp;quot;:5,&amp;quot;range&amp;quot;:&amp;quot;3484-4758&amp;quot;} --&gt;
&lt;h2 class=&quot;sectionedit6&quot; id=&quot;math&quot;&gt;math&lt;/h2&gt;
&lt;div class=&quot;level2&quot;&gt;

&lt;p&gt;
as mentioned at the beginning, there is “more mathematical” solution to this problem. not to go into details, it is based on the &lt;a href=&quot;http://wazniak.mimuw.edu.pl/index.php?title=Matematyka_dyskretna_1/%C4%86wiczenia_8:_Funkcje_tworz%C4%85ce_w_zliczaniu_obiekt%C3%B3w_kombinatorycznych&quot; class=&quot;urlextern&quot; title=&quot;http://wazniak.mimuw.edu.pl/index.php?title=Matematyka_dyskretna_1/%C4%86wiczenia_8:_Funkcje_tworz%C4%85ce_w_zliczaniu_obiekt%C3%B3w_kombinatorycznych&quot; rel=&quot;ugc nofollow&quot;&gt;generating functions&lt;/a&gt;.
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;math&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;math&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:3,&amp;quot;secid&amp;quot;:6,&amp;quot;range&amp;quot;:&amp;quot;4759-5084&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit7&quot; id=&quot;first_approach1&quot;&gt;first approach&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
here is an example solution, using the above mentioned math-based results (&lt;a href=&quot;https://www.baszerr.eu/lib/exe/fetch.php?media=blog:2013:12:02:ftw.cpp&quot; class=&quot;media mediafile mf_cpp&quot; title=&quot;blog:2013:12:02:ftw.cpp (575 B)&quot;&gt;download&lt;/a&gt;):
&lt;/p&gt;
&lt;pre class=&quot;code cpp&quot;&gt;&lt;span class=&quot;co1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt;      Number&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;typedef&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;long&lt;/span&gt; Count&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw2&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt; CFN&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;
Count cntFor&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; Number n&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;CFN&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; cntFor&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;CFN&lt;span class=&quot;sy2&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; cntFor&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;CFN&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;sy2&quot;&gt;-&lt;/span&gt;CFN&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; cntFor&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;CFN&lt;span class=&quot;sy2&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw2&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;
Count cntFor&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; Number n&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw2&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;
Count cntFor&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; Number n&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;sy1&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;sy1&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; cntFor&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;sy2&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy2&quot;&gt;+&lt;/span&gt; cntFor&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;sy2&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; main&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; Number i &lt;span class=&quot;sy1&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw3&quot;&gt;cout&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; i &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot; == &amp;quot;&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; cntFor&lt;span class=&quot;sy1&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;sy1&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy1&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; endl&lt;span class=&quot;sy4&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
now that&amp;#039;s short. and fast. this one executes in ~2.4[s] on my machine. this is probably the solution you&amp;#039;ll find on the internet. and yet.. can we do any better?
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;first approach&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;first_approach1&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:3,&amp;quot;secid&amp;quot;:7,&amp;quot;range&amp;quot;:&amp;quot;5085-5917&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit8&quot; id=&quot;caching&quot;&gt;caching&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
if you look close enough, i&amp;#039;ll see that a lot of these recursive calls are repeated over and over again. smells like fibonacci&amp;#039;s recursive implementation – the same code with the same parameters over and over again. first idea? add cache (&lt;a href=&quot;https://www.baszerr.eu/lib/exe/fetch.php?media=blog:2013:12:02:ftw-cache.cpp&quot; class=&quot;media mediafile mf_cpp&quot; title=&quot;blog:2013:12:02:ftw-cache.cpp (866 B)&quot;&gt;download&lt;/a&gt;).
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;&lt;span class=&quot;co1&quot;&gt;// ... the same as previously ...&lt;/span&gt;
constexpr Number   value   &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
constexpr &lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt; maxCoin &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;512&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span class=&quot;kw4&quot;&gt;typedef&lt;/span&gt; vector&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Count&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; Internal&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;typedef&lt;/span&gt; vector&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Internal&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt; External&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
External cache&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; maxCoin&lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; Internal&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;value&lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;unsigned&lt;/span&gt; CFN&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
Count cntFor&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;const&lt;/span&gt; Number n&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;cache&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;CFN&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;n&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; cache&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;CFN&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;n&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;CFN&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; cache&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;CFN&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;n&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; cntFor&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;CFN&lt;span class=&quot;sy0&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;kw1&quot;&gt;return&lt;/span&gt; cache&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;CFN&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt;n&lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;=&lt;/span&gt; cntFor&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;CFN&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;sy0&quot;&gt;-&lt;/span&gt;CFN&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;+&lt;/span&gt; cntFor&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;CFN&lt;span class=&quot;sy0&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;n&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;co1&quot;&gt;// ... the same as previously ...&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
new time? 0.004[s] (4 milliseconds!). now that&amp;#039;s nice. :D
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;caching&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;caching&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:4,&amp;quot;secid&amp;quot;:8,&amp;quot;range&amp;quot;:&amp;quot;5918-6783&amp;quot;} --&gt;
&lt;h3 class=&quot;sectionedit9&quot; id=&quot;can_we_do_better&quot;&gt;can we do better?&lt;/h3&gt;
&lt;div class=&quot;level3&quot;&gt;

&lt;p&gt;
yes. although 4[ms] was good enough for me for one day… ;)
&lt;/p&gt;

&lt;p&gt;
if you look closely, you can easily optimize the cache size. the one above has short syntax, but uses way more memory than is really needed. you could go down to ~value*10*sizeof(number)==O(value) bytes. in fact – you could simply provide the counting table, with all the required coefficients, and so compute in O(N) time and memory. that would be both nice and fast at the same time. ;)
&lt;/p&gt;

&lt;p&gt;
!! SPOILER ALERT !!
&lt;/p&gt;

&lt;p&gt;
the answer is 2320518947 – better than sheep counting!
&lt;/p&gt;

&lt;/div&gt;
&lt;!-- EDIT{&amp;quot;target&amp;quot;:&amp;quot;section&amp;quot;,&amp;quot;name&amp;quot;:&amp;quot;can we do better?&amp;quot;,&amp;quot;hid&amp;quot;:&amp;quot;can_we_do_better&amp;quot;,&amp;quot;codeblockOffset&amp;quot;:5,&amp;quot;secid&amp;quot;:9,&amp;quot;range&amp;quot;:&amp;quot;6784-&amp;quot;} --&gt;</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:09:06 +0000</pubDate>
        </item>
    </channel>
</rss>
