向前或向后写入时相同的数字称为回文数。例如,1234321是回文数。所有单数数字都是回文数字。虽然回文数在十进制中最常被考虑,但回文性的概念可以适用于任何数字系统中的自然数。考虑基b≥2中的一个数字N>0,其中它是用标准表示法编写的,用k+1位数字(i)作为∑i=0k(ibi)。在这里,和往常一样,0≤ai<b对于所有i和k是非零的。然后N是回文的当且仅当i=ak−i对于所有i.0在任何基中都是0,并且定义上也是回文的。给定任何正十进制整数N和基b,您应该知道N是否是基b中的回文数。
Input Specification:
Each input file contains one test case. Each case consists of two positive numbers N and b, where 0<N≤10
9
is the decimal number and 2≤b≤10
9
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 "a
k
a
k−1
… a
0
". 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
具体思路
就是数制转换,再加上个回文数判断,,很简单,因为数值大,我就用的python
具体代码
def main():
y=input('').split( )
ben=int(y[0])
cha=int(y[1])
s=[]
sum=1
while ben!=0:#基本数制转换方法
sum=ben%cha
s.append(sum)
ben=ben//cha
j=len(s)-1
i=0
biaoji=0
while j-i>1:#判断是否为回文数
if(s[j]!=s[i]):
biaoji=1
j=j-1
i=i+1
if len(s)==2:
if(s[j]!=s[i]):
biaoji=1
if biaoji==1:
print("No")
else:
print("Yes")
s=s[::-1]#记得逆序输出
for i in range(len(s)):
if i!=0:
print(' ',end='')
print(str(s[i]),end='')
main()
很简单