1019. General Palindromic Number (20)

Source: Internet
Author: User

1019. General Palindromic Number (20)

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 numbers in any numeral system. consider a number N> 0 in base B> = 2, where it is written in standard notation with k + 1 digits ai as the sum of (aibi) for I from 0 to k. here, as usual, 0 <= ai <B for all I and ak is non-zero. then N is palindromic if and only if ai = ak-I for all I. zero is written 0 in any base and is also palindromic by definition.

Given any non-negative decimal integer N and a base B, you are supposed to tell if N is a palindromic number in base B.

Input Specification:

Each input file contains one test case. each case consists of two non-negative numbers N and B, where 0 <= N <= 109 is the decimal number and 2 <= B <= 109 is the base. the numbers are separated by a space.

Output Specification:

For each test case, first print in one line "Yes" if N is a palindromic number in base B, or "No" if not. then in the next line, print N as the number in base B in the form "ak ak-1... a0 ". notice that there must be no extra space at the end of output.

Sample Input 1:
27 2
Sample Output 1:
Yes
1 1 0 1 1
Sample Input 2:
121 5
Sample Output 2:
No

4 4 1

#include 
 
  using namespace std;int result[100000000];long i, j;bool isPalindromic(int *result) {    long begin, end;    for(begin=0, end=i-1; begin<=end; begin++, end--) {        if(result[begin]!=result[end]) {            return false;        }    }    return true;}void change(long a, long b) {    i = 0;    while(a != 0) {        result[i++] = a % b;        a /= b;    }}int main() {    int a, b;    while(cin>>a>>b) {        change(a, b);        if(isPalindromic(result)) {            cout<<"Yes"<
  
   0 ; j--) {            cout<
   
    

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.