Palindrome in base n
InterviewTime limit1sMemory limit256 MB
For each query, convert the decimal number A to base n and report whether its digit sequence reads the same forwards and backwards.
- Level
Easy3 of 10
- Topics
- Math, Implementation, Two pointers, Brute force
- Solved
- No attempts yet
Problem
Base n notation writes a number with radix . Decimal notation, for example, uses radix 10. A number written in base n has the value
For example, the decimal number 12468 equals .
A palindrome is text that reads the same forwards and backwards. madam, level, and 12321 are palindromes. Chung-ang, university, and 54899 are not.
Given a decimal number and a radix , write a program that decides whether the base n representation of is a palindrome. Each position counts as a single symbol, and the representation carries no leading zero. In other words, the representation is a palindrome when reversing the sequence of digit values leaves it unchanged.
Input
The first line contains the number of test cases ().
Each of the next lines contains a decimal number () and a radix (), separated by a space.
Output
For each test case, print 1 on its own line if the base n representation of is a palindrome, and 0 otherwise.