Skip to content

1808

A_Lucky_Numbers

Explanation

Hateehc is a Martian blogger who wants to buy a starship with the luckiest number. The luckiness of a number is the difference between its largest and smallest digits.

For example, the luckiness of the number 142857 is 8 - 1 = 7, and the luckiness of the number 111 is 0 because all its digits are the same.

In a store, there are starships with numbers from 𝑙 to 𝑟. You need to help Hateehc find the starship with the luckiest number.

Logic to solve the problem:

  1. Iterate through the range of starship numbers [𝑙, 𝑟].
  2. For each number in the range, find the luckiness (the difference between the largest and smallest digits).
  3. Keep track of the maximum luckiness found so far and the corresponding starship number.
  4. Stop the iteration if the maximum luckiness is equal to 9, as this is the highest possible luckiness.
  5. Return the starship number with the highest luckiness.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def luck(n):
    digits = [int(d) for d in str(n)]
    return max(digits) - min(digits)


def solve():
    l, r = map(int, input().split())
    res = l
    max_luck = luck(l)

    for i in range(l + 1, min(r + 1, l + 101)):
        curr_luck = luck(i)
        if curr_luck > max_luck:
            max_luck = curr_luck
            res = i

        if max_luck == 9:
            break

    print(res)


for _ in range(int(input())):
    solve()

B_Playing_in_a_Casino

Explanation

The winnings for each game are calculated as the sum of the absolute differences between the corresponding numbers on the two players' cards.

  1. For each column in the grid, sort the values.
  2. Iterate through the sorted values and calculate the accumulated differences between each value and the previous one.
  3. Sum the accumulated differences for each column.
  4. Divide the total sum by 2 to get the total winnings.

Calculate the total winnings for all games played in pairs. Sort the numbers in each column of the grid and calculates the accumulated differences between consecutive sorted numbers.

The total winnings are obtained by summing up the accumulated differences and dividing the result by 2.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def solve():
    n, m = map(int, input().split())
    cards = [list(map(int, input().split())) for _ in range(n)]

    winnings = 0
    for j in range(m):
        sorted_col = sorted(cards[i][j] for i in range(n))
        accumulated_diff = 0
        diff_sum = 0

        for i in range(1, n):
            accumulated_diff += (sorted_col[i] - sorted_col[i - 1]) * i
            diff_sum += accumulated_diff

        winnings += diff_sum

    print(winnings)


for _ in range(int(input())):
    solve()