{"id":208,"date":"2013-04-22T14:08:57","date_gmt":"2013-04-22T06:08:57","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=208"},"modified":"2013-04-22T14:08:57","modified_gmt":"2013-04-22T06:08:57","slug":"1019-general-palindromic-number-20","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=208","title":{"rendered":"1019. General Palindromic Number (20)"},"content":{"rendered":"<p>A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.<br \/>\nAlthough palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number N &gt; 0 in base b &gt;= 2, where it is written in standard notation with k+1 digits a<sub>i<\/sub>\u00a0as the sum of (a<sub>i<\/sub>b<sup>i<\/sup>) for i from 0 to k. Here, as usual, 0 &lt;= a<sub>i<\/sub>\u00a0&lt; b for all i and a<sub>k<\/sub>\u00a0is non-zero. Then N is palindromic if and only if a<sub>i<\/sub>\u00a0= a<sub>k-i<\/sub>\u00a0for all i. Zero is written 0 in any base and is also palindromic by definition.<br \/>\nGiven any non-negative decimal integer N and a base b, you are supposed to tell if N is a palindromic number in base b.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. Each case consists of two non-negative numbers N and b, where 0 &lt;= N &lt;= 10<sup>9<\/sup>\u00a0is the decimal number and 2 &lt;= b &lt;= 10<sup>9<\/sup>\u00a0is the base. The numbers are separated by a space.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each test case, first print in one line &#8220;Yes&#8221; if N is a palindromic number in base b, or &#8220;No&#8221; if not. Then in the next line, print N as the number in base b in the form &#8220;a<sub>k<\/sub>\u00a0a<sub>k-1<\/sub>\u00a0&#8230; a<sub>0<\/sub>&#8220;. Notice that there must be no extra space at the end of output.<br \/>\n<b>Sample Input 1:<\/b><\/p>\n<pre>27 2<\/pre>\n<p><b>Sample Output 1:<\/b><\/p>\n<pre>Yes\n1 1 0 1 1<\/pre>\n<p><b>Sample Input 2:<\/b><\/p>\n<pre>121 5<\/pre>\n<p><b>Sample Output 2:<\/b><\/p>\n<pre>No\n4 4 1<\/pre>\n<p>===========================<br \/>\n\u6700\u8fd1\u4ea7\u91cf\u592a\u4f4e\uff0c\u90fd\u662f\u6709\u5c0fbug\u7684\u7b54\u6848\uff0c\u552f\u72ec\u8fd9\u9053\u9898\u7ec8\u4e8e\u3002\u3002\u3002\u3002<br \/>\n\u5176\u5b9e\u8fd9\u9053\u9898\u76f8\u5f53\u7b80\u5355\uff0c\u4e0d\u60f3\u8bf4\u5565\u4e86<br \/>\n\u65f6\u95f4\u7a7a\u95f4\u90fd\u6ca1\u6709\u9650\u5236\uff0c\u5982\u679c\u673a\u8bd5\u78b0\u5230\u8fd9\u79cd\u9898\uff0c\u90a3\u4f30\u8ba1\u662f\u516b\u8f88\u5b50\u4fee\u6765\u7684\u798f\u3002\u3002\u3002<br \/>\n\u76f4\u63a5\u8d34\u7b54\u6848\u5427\u3002\u3002<br \/>\n===========================<\/p>\n<pre>\n#include <iostream>\nusing namespace std;\nlong resultSize = 0;\nconst int MAX_BUFF_SIZE = 30;\nint buff[MAX_BUFF_SIZE];\nvoid calcBase(long inputVal, long base)\n{\n\t\/\/\u8ba1\u7b97\u4ee5base\u4e3a\u4f4e\u7684inputVal\u7684\u503c\n\tif(inputVal == 0)\n\t{\n\t\tresultSize = 1;\n\t\tbuff[0] = 0;\n\t\treturn;\n\t}\n\tresultSize = 0;\n\twhile(inputVal > 0)\n\t{\n\t\tbuff[resultSize++] = inputVal % base;\n\t\tinputVal \/= base;\n\t}\n}\nbool isPalindrome()\n{\n\tlong limit = resultSize \/2;\n\tfor(long i=0; i<limit; i++)\n\t{\n\t\tif( buff[i] != buff[resultSize-i -1] )\n\t\t\treturn false;\n\t}\n\treturn true;\n}\nint main()\n{\n\tlong a,b;\n\tcin >> a >> b;\n\tcalcBase(a, b);\n\tif( isPalindrome())\n\t\tcout << \"Yes\" << endl;\n\telse\n\t\tcout << \"No\" << endl;\n\twhile(--resultSize)\n\t{\n\t\tcout << buff[resultSize] << \" \";\n\t}\n\tcout << buff[0];\n\treturn 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[84],"class_list":["post-208","post","type-post","status-publish","format-standard","hentry","category-study","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=208"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/208\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}