-1

How to count many palindrome number that have total digits is divisible by 10 from L to R (1 <= L <= R <= 10^12) in 0.5s? My idea: generate palindrome number then count. But it is TLE. Here is my code:

def circle_num(x):
    t = 0
    for digit in str(x):
        t += int(digit)
    if t % 10 == 0:
        return True
    else:
        return False
def palinstr_evendig(half):
    return half + half[::-1]
def palinstr_odddig(h):
    return h + (h[::-1])[1:]
def num_palin_circle_num_1to(t, k):
    r = 0; j = 1
    while not int(palinstr_odddig(str(j))) > k:
        a = int(palinstr_evendig(str(j)))
        if (not a > k) and (not a < t) and (circle_num(a)):
            r += 1
        b = int(palinstr_odddig(str(j)))
        if (not b > k) and (not b < t) and (circle_num(b)):
            r += 1
        j += 1
    return r
s = input()
l, r = map(int, s.split())
print(num_palin_circle_num_1to(l, r))

And I don't have any idea to implement. Python Language

Can anyone help me? Thanks very much.

1 Answers1

1

I'm assuming you're counting the palindromes L ≤ x < R. If R is inclusive, just replace R with R + 1 below.

Suppose we had a function f(N) which counts the number of palindromes that fits your criteria such that 0 ≤ x < N. Then we'd just need to calculate F(R) - F(L). [Or f(R + 1) - f(L) is R is inclusive].

So let's look at even palindromes and odd palindromes.

  • An even palindrome, say ABCDEEDCBA. No matter what we have ABCD be, there are two values of E such that A+B+C+D+E is a multiple of 5.
  • An odd palindrome, say ABCDEDCBA. No matter what we have ABCD be, there is precisely one value of E (and it's even!) such that E + 2(A + B + C + D) is a multiple of 10.

Say we want to find f(314159).

  • There is 1 palindrome of length 1 (0)
  • There is 1 palindrome of length 2 (55)
  • There are 9 palindromes of length 3. (ABA, where 1 ≤ A ≤ 9)
  • There are 18 = 9 x 2 palindromes of length 4. (ABBA, where 1 ≤ A ≤ 9)
  • There are 90 palindromes of length 5. (ABCBA, 10 ≤ AB ≤ 99)
  • In the range 100,000 ≤ x < 310,000, the palindromes are of the form ABCCBA, where (10 ≤ AB ≤ 30) and C can have one of two values. So there are 21 x 2 of them.
  • For the last digit, you have to look at them individually. 311113, 312213, and 313313 aren't multiples of ten. 314413 is both not a multiple of ten and too big.

So you can calculate f(n) in time linear to the number of digits in n.


Implementing this isn't that hard. I'm going to assume that you're trying to calculate f(N), where N is an n-digit number. The case of N < 100 is trivial (is 0 < N? is 55 < N?), so I'm going to assume n≥3.

How many 10-palindromes are there of length m, where 1 ≤ m < n?

  • m = 1: 1
  • m = 2: 1
  • m odd and m > 2: 9 * 10 ** ((m - 3) // 2)
  • m even and m > 2: 9 * 2 * 10 ** ((m - 4) // 2

How many 10-palindromes are there of length n that are less than N? For length n. Let's suppose N is AB...DED...BA if n is odd and AB...DEED...BA if n is even.

  • For each value 10...0 ≤ ab...d < AB...D, there are 2 palindromes if n is even and 1 palindrome if n is odd. That's a subtraction and maybe a multiplication.
  • Finally, for AB...D, there are 2 possibile values for E if n is even and 1 possible value if n is odd. You have to check these two values to see if they are actually < N.

This is about twenty lines of code.

Frank Yellin
  • 9,127
  • 1
  • 12
  • 22