{"id":1443,"date":"2013-04-03T02:01:45","date_gmt":"2013-04-02T18:01:45","guid":{"rendered":"https:\/\/blog.rexdf.org\/?p=1443"},"modified":"2013-04-03T02:01:45","modified_gmt":"2013-04-02T18:01:45","slug":"codeforces-177-e","status":"publish","type":"post","link":"https:\/\/blog.rexdf.org\/en\/2013\/04\/codeforces-177-e\/","title":{"rendered":"Codeforces 177 E"},"content":{"rendered":"<div>\n<div>\u00a0Polo the Penguin and XOR operation<\/div>\n<div>\n<div>time limit per test<\/div>\n<p>2 seconds<\/p><\/div>\n<div>\n<div>memory limit per test<\/div>\n<p>256 megabytes<\/p><\/div>\n<div>\n<div>input<\/div>\n<p>standard input<\/p><\/div>\n<div>\n<div>output<\/div>\n<p>standard output<\/p><\/div>\n<\/div>\n<div>\n<p>Little penguin Polo likes permutations. But most of all he likes permutations of integers from\u00a00\u00a0to\u00a0<i>n<\/i>, inclusive.<\/p>\n<p>For permutation\u00a0<i>p<\/i>\u2009=\u2009<i>p<\/i><sub>0<\/sub>,\u2009<i>p<\/i><sub>1<\/sub>,\u2009&#8230;,\u2009<i>p<\/i><sub><i>n<\/i><\/sub>, Polo has defined its beauty \u2014 number\u00a0<img decoding=\"async\" class=\"ls_lazyimg\" alt=\"\" src=\"https:\/\/static.rexdf.org\/plugins\/images-lazyload-and-slideshow\/blank_250x250.gif\" file=\"http:\/\/codeforces.ru\/renderer\/0c51fe5d835015fdb8b8dbb056e9a83c439c83dc.png\" align=\"middle\" \/><noscript><img decoding=\"async\" alt=\"\" src=\"http:\/\/codeforces.ru\/renderer\/0c51fe5d835015fdb8b8dbb056e9a83c439c83dc.png\" align=\"middle\" \/><\/noscript>.<\/p>\n<p>Expression\u00a0<img decoding=\"async\" class=\"ls_lazyimg\" alt=\"\" src=\"https:\/\/static.rexdf.org\/plugins\/images-lazyload-and-slideshow\/blank_250x250.gif\" file=\"http:\/\/codeforces.ru\/renderer\/b364f2e04c665b78b924ec10666327a4ef4635bc.png\" align=\"middle\" \/><noscript><img decoding=\"async\" alt=\"\" src=\"http:\/\/codeforces.ru\/renderer\/b364f2e04c665b78b924ec10666327a4ef4635bc.png\" align=\"middle\" \/><\/noscript>\u00a0means applying the operation of bitwise excluding &#8220;OR&#8221; to numbers\u00a0<i>x<\/i>\u00a0and\u00a0<i>y<\/i>. This operation exists in all modern programming languages, for example, in language\u00a0C++\u00a0and\u00a0Java\u00a0it is represented as &#8220;^&#8221; and in\u00a0Pascal\u00a0\u2014 as &#8220;xor&#8221;.<\/p>\n<p>Help him find among all permutations of integers from\u00a00\u00a0to\u00a0<i>n<\/i>\u00a0the permutation with the maximum beauty.<\/p>\n<\/div>\n<div>\n<div>Input<\/div>\n<p>The single line contains a positive integer\u00a0<i>n<\/i>\u00a0(1\u2009\u2264\u2009<i>n<\/i>\u2009\u2264\u200910<sup>6<\/sup>).<\/p>\n<\/div>\n<div>\n<div>Output<\/div>\n<p>In the first line print integer\u00a0<i>m<\/i>\u00a0the maximum possible beauty. In the second line print any permutation of integers from\u00a00\u00a0to\u00a0<i>n<\/i>\u00a0with the beauty equal to\u00a0<i>m<\/i>.<\/p>\n<p>If there are several suitable permutations, you are allowed to print any of them.<\/p>\n<\/div>\n<div>\n<div>Sample test(s)<\/div>\n<div>\n<div>\n<div>input<\/div>\n<pre>4<\/pre>\n<\/div>\n<div>\n<div>output<\/div>\n<pre>20 0 2 1 4 3<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"brush: actionscript3; gutter: true\">\r\n#include &lt;iostream&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;cstdlib&gt;\r\n\r\nusing namespace std;\r\n\r\nint a[1000005];\r\nint vis[1000005];\r\n\r\nint main()\r\n{\r\n    int n,mask=0,tmp_p,p=1;\r\n    int tmp;\r\n    long long sum=0;\r\n    cin&gt;&gt;n;\r\n    while(p&lt;=n)\r\n    {\r\n        mask|=p;\r\n        p&lt;&lt;=1;\r\n    }\r\n    \/\/cout&lt;&lt;mask&lt;&lt;endl;\r\n    for(int i=n;i;i--)\r\n      if(0==vis[i])\r\n      {\r\n          tmp=((~i)&amp;mask);\r\n          \/\/cout&lt;&lt;i&lt;&lt;&quot; find &quot;&lt;&lt;tmp&lt;&lt;endl;\r\n          if(tmp&lt;=n)\r\n          {\r\n              vis[i]=1;\r\n              vis[tmp]=1;\r\n              sum+=mask&lt;&lt;1;\r\n              a[i]=tmp;\r\n              a[tmp]=i;\r\n          }\r\n          else\r\n          {\r\n              tmp_p=1;\r\n              \/\/tmp=((~i)&amp;(mask&gt;&gt;1));\r\n              while((1&lt;&lt;tmp_p)&lt;=mask)\r\n              {\r\n                  tmp=((~i)&amp;(mask&gt;&gt;tmp_p));\r\n                  if(0==vis[tmp])break;\r\n                  tmp_p++;\r\n              }\r\n              vis[tmp]=1;\r\n              vis[i]=1;\r\n              sum+=(tmp^i)&lt;&lt;1;\r\n              a[i]=tmp;\r\n              a[tmp]=i;\r\n          }\r\n      }\r\n    cout&lt;&lt;sum&lt;&lt;endl;\r\n    if(vis[0])cout&lt;&lt;a[0];\r\n    else cout&lt;&lt;0;\r\n    for(int i=1;i&lt;=n;i++)\r\n      cout&lt;&lt;&quot; &quot;&lt;&lt;a[i];\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0Polo the Penguin and XOR operation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little penguin Polo likes permutations. But most of all he likes permutations of integers from\u00a00\u00a0to\u00a0n, inclusive. &hellip; <a href=\"https:\/\/blog.rexdf.org\/en\/2013\/04\/codeforces-177-e\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[125],"class_list":["post-1443","post","type-post","status-publish","format-standard","hentry","category-algorithm","tag-codeforces"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts\/1443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/comments?post=1443"}],"version-history":[{"count":0,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/posts\/1443\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/media?parent=1443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/categories?post=1443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.rexdf.org\/en\/wp-json\/wp\/v2\/tags?post=1443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}