The Clock (Codeforces 1692D)
Tags: Implement, brute force

Victor has a 24-hour clock that shows the time in the format "HH:MM" (00 $$$\le$$$ HH $$$\le$$$ 23, 00 $$$\le$$$ MM $$$\le$$$ 59). He looks at the clock every $$$x$$$ minutes, and the clock is currently showing time $$$s$$$. How many different palindromes will Victor see in total after looking at the clock every $$$x$$$ minutes, the first time being at time $$$s$$$?For example, if the clock starts out as 03:12 and Victor looks at the clock every $$$360$$$ minutes (i.e. every $$$6$$$ hours), then he will see the times 03:12, 09:12, 15:12, 21:12, 03:12, and the times will continue to repeat. Here the time 21:12 is the only palindrome he will ever see, so the answer is $$$1$$$.A palindrome is a string that reads the same backward as forward. For example, the times 12:21, 05:50, 11:11 are palindromes but 13:13, 22:10, 02:22 are not.

Input: The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The description of each test case follows.The only line of each test case contains a string $$$s$$$ of length $$$5$$$ with the format "HH:MM" where "HH" is from "00" to "23" and "MM" is from "00" to "59" (both "HH" and "MM" have exactly two digits) and an integer $$$x$$$ ($$$1 \leq x \leq 1440$$$) — the number of minutes Victor takes to look again at the clock.

Output: For each test case, output a single integer — the number of different palindromes Victor will see if he looks at the clock every $$$x$$$ minutes starting from time $$$s$$$.

Note: The first test case is explained in the statement.

Sample Inputs:
6
03:12 360
00:00 1
13:22 2
15:15 10
11:11 1440
22:30 27

Sample Outputs:
1
16
10
0
1
1

Solution (Python):
from math import gcd
palindromes = [int(s.split(':')[0]) * 60 + int(s.split(':')[1]) for s in ['00:00', '01:10', '02:20', '03:30', '04:40', '05:50', '10:01', '11:11', '12:21', '13:31', '14:41', '15:51', '20:02', '21:12', '22:22', '23:32']]
for _ in range(int(input())):
    c = 0
    (s, x) = input().split(' ')
    s = int(s.split(':')[0]) * 60 + int(s.split(':')[1])
    x = int(x)
    for p in palindromes:
        if (p - s) % 1440 % gcd(x, 1440) == 0:
            c += 1
    print(c)

Solution (Python):
import math
t = int(input())
palindroms = []
for i in range(24):
    if i % 10 < 6:
        if i < 10:
            e = '0' + str(i)
            a = str(i % 10) + str(i // 10)
            palindroms.append(e + a)
        else:
            a = i % 10 * 10 + i // 10
            palindroms.append(str(i * 100 + a))
for i in range(t):
    (s, y) = input().split()
    (h, m) = s.split(':')
    time = int(h) * 60 + int(m)
    x = int(y)
    otvet = 0
    min_step = math.gcd(x, 1440)
    for j in range(len(palindroms)):
        if (int(palindroms[j]) % 100 + int(palindroms[j]) // 100 * 60 - time) % min_step == 0:
            otvet += 1
    print(otvet)

Solution (Python):
import sys
input = sys.stdin.readline
t = int(input())

def solve(s1):
    answer = 0
    (s, x) = map(str, input().strip().split())
    x = int(x)
    (h, m) = s.split(':')
    (h, m) = (int(h), int(m))
    m = 60 * h + m
    s2 = set()
    for _ in range(1441):
        if m in s1 and m not in s2:
            answer += 1
            s2.add(m)
        m = (m + x) % 1440
    print(answer)
s = set()
for i in range(24):
    m = int(str(i).zfill(2)[::-1])
    if m < 60:
        s.add(i * 60 + m)
for _ in range(t):
    solve(s)