GESP Practice Problems: Reading, Scheduling, Geometry, and Bit Patterns

Holiday Reading

A book has n pages. A student can read at most k pages per day over t vacation days. The maximum number of pages they can finish is the smaller of n and k * t.

n = int(input())
k = int(input())
t = int(input())
print(min(n, k * t))

Shared Duty Schedule

Two students clean on cycles of m and n days. The next time they coincide is the least common multiple (LCM) of m and n. Since LCM(a, b) = a × b / GCD(a, b), but given small constraints, brute-force works:

a = int(input())
b = int(input())
for i in range(max(a, b), a * b + 1):
    if i % a == 0 and i % b == 0:
        print(i)
        break

Counting Integer-Area Right Triangles

For legs a and b (1 ≤ a, b ≤ n), the area a*b/2 is integer iff a*b is even. To avoid double-counting symmetric pairs like (a,b) and (b,a), iterate with b ≤ a:

n = int(input())
count = 0
for a in range(1, n + 1):
    for b in range(1, a + 1):
        if (a * b) % 2 == 0:
            count += 1
print(count)

Power-Sum Numbers

A number is a "power-sum" if it equals 2^x + 2^y for non-negative integers x, y. Equivalently, it has exactly two bits set in its binary representation (or one bit if x=y, but 2^x + 2^x = 2^(x+1), which is a single power—so only distinct powers count unless specified). However, note that 2 = 1+1 = 2^0 + 2^0 is allowed per sample (output includes 2).

Thus, valid numbers are those with either:

  • Exactly two '1' bits in binary (e.g., 3=11₂, 5=101₂, 6=110₂), or
  • One '1' bit but interpreted as sum of two equal powers (e.g., 2 = 1+1, 4 = 2+2, etc.). But 2^x + 2^x = 2^(x+1), so these are just powers of two ≥2.

However, the sample input 2 8 yields 6 values: 2,3,4,5,6,8. Indeed:

  • 2 = 1+1
  • 3 = 1+2
  • 4 = 2+2
  • 5 = 1+4
  • 6 = 2+4
  • 8 = 4+4

So both cases are included. A number qualifies if it can be written as sum of two (not necessarily distinct) powers of two.

An efficient check: for each candidate n, test all splits i from 1 to n//2, and verify both i and n-i are powers of two. A number x is a power of two iff x & (x - 1) == 0 and x > 0.

l, r = map(int, input().split())
total = 0
for num in range(l, r + 1):
    found = False
    for part in range(1, num // 2 + 1):
        other = num - part
        if part > 0 and other > 0:
            if (part & (part - 1)) == 0 and (other & (other - 1)) == 0:
                found = True
                break
    if found:
        total += 1
print(total)

N-Shape Matrix

Construct an m×m matrix (odd m) where '+' appears in column 0, column m-1, and the main diagonal (row index == column index). All other positions are '-'.

m = int(input())
for i in range(m):
    for j in range(m):
        if j == 0 or j == m - 1 or i == j:
            print('+', end='')
        else:
            print('-', end='')
    print()

Tags: python GESP algorithms Number Theory combinatorics

Posted on Sun, 07 Jun 2026 17:44:57 +0000 by cmanhatton