आसान 0 हल किए गए 0% स्वीकृति

Double-Base Palindromes

Add up the numbers that read the same both ways in base 10 and in a second base.

आप क्या सीखेंगे

  • Convert a number to any base
  • Check whether a sequence is a palindrome
  • Combine two conditions cheaply

समस्या

अनुवाद दिखाएँ

A palindrome reads the same forwards and backwards. 33 is a palindrome in base 10, and in base 2 it is 100001, a palindrome again. Add up every number from 1 to limit that is a palindrome both in base 10 and in base base. Numbers are written without leading zeros.

Implement double_base_palindromes(limit, base).

Input

limit: The largest number to check.

base: The second base.

Output

Return the sum of all such numbers.

सीमाएँ

1 ≤ limit ≤ 100,000; 2 ≤ base ≤ 16

उदाहरण

उदाहरण 1
इनपुट:
limit = 10
base = 2

अपेक्षित आउटपुट:
expected = 25
इनपुट:
long long limit = 10;
long long base = 2;

अपेक्षित आउटपुट:
long long expected = 25;
इनपुट:
long limit = 10;
long base = 2;

अपेक्षित आउटपुट:
long expected = 25;

व्याख्या

1, 3, 5, 7 and 9 are 1, 11, 101, 111 and 1001 in base 2, all palindromes; 2, 4, 6 and 8 are not. Sum 25.

उदाहरण 2
इनपुट:
limit = 50
base = 3

अपेक्षित आउटपुट:
expected = 15
इनपुट:
long long limit = 50;
long long base = 3;

अपेक्षित आउटपुट:
long long expected = 15;
इनपुट:
long limit = 50;
long base = 3;

अपेक्षित आउटपुट:
long expected = 15;

व्याख्या

In base 3: 1, 2, 4 (11) and 8 (22) qualify, while 33 is 1020 and 44 is 1122. Sum 15.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Repeated division by the base gives the digits from last to first.

संकेत 2

A palindrome check compares the digit list with its reverse.

संकेत 3

Check base 10 first: few numbers pass it, so the second conversion is rarely needed.

अपेक्षित जटिलता

O(limit × log limit) time, O(log limit) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।