programming
Challenges

Sums

Find the sum of nums[i] for i in [l, r] (if there are any issues with input/output format, plz open a ticket)
#!/usr/bin/env python3
import random
import subprocess
import sys
import time
start = time.time()
n = 123456
nums = [str(random.randint(0, 696969)) for _ in range(n)]
print(' '.join(nums), flush=True)
ranges = []
for _ in range(n):
l = random.randint(0, n - 2)
r = random.randint(l, n - 1)
ranges.append(f"{l} {r}") #inclusive on [l, r] 0 indexed
print(l, r)
big_input = ' '.join(nums) + "\n" + "\n".join(ranges) + "\n"
proc = subprocess.Popen(
['./solve'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = proc.communicate(input=big_input)
out_lines = stdout.splitlines()
ans = [int(x) for x in out_lines[:n]]
urnums = []
for _ in range(n):
urnums.append(int(input()))
if ans != urnums:
print("wawawawawawawawawa")
sys.exit(1)
if time.time() - start > 10:
print("tletletletletletle")
sys.exit(1)
print(open('flag.txt', 'r').readline())
tbh....you could treat it like a leetcode challenge.
from pwn import *
HOST = "play.scriptsorcerers.xyz"
PORT = 10190
def solve():
io = remote(HOST, PORT)
# Receive the list of numbers
nums = list(map(int, io.recvline(timeout=10).strip().split()))
n = len(nums)
# Build prefix sums
prefix_sums = [0] * (n + 1)
for i in range(n):
prefix_sums[i + 1] = prefix_sums[i] + nums[i]
# Answer n queries
for _ in range(n):
l, r = map(int, io.recvline(timeout=10).strip().split())
io.sendline(str(prefix_sums[r + 1] - prefix_sums[l]))
# Get the flag
print(io.recvall(timeout=10).decode().strip())
solve()
❯ python solve.py
[+] Opening connection to play.scriptsorcerers.xyz on port 10190: Done
/home/x/code/ctfs/script/ppc/sums/solve.py:21: BytesWarning: Text is not bytes; assuming ASCII, no guarantees. See https://docs.pwntools.com/#bytes
io.sendline(str(prefix_sums[r + 1] - prefix_sums[l]))
[+] Receiving all data: Done (42B)
[*] Closed connection to play.scriptsorcerers.xyz port 10190
scriptCTF{1_w4n7_m0r3_5um5_29a06dcb2e10}
scriptCTF{1_w4n7_m0r3_5um5_3b287c7e69da}
More Divisors

find length of the longest subsequence with gcd > 1 :)
from pwn import remote
import math
from collections import defaultdict
from tqdm import tqdm
def gcd(a, b):
while b:
a, b = b, a % b
return a
def gcd_of_list(numbers):
if not numbers:
return 0
result = numbers[0]
for i in range(1, len(numbers)):
result = gcd(result, numbers[i])
if result == 1:
return 1
return result
def get_prime_factors(n):
factors = set()
# Check for 2
while n % 2 == 0:
factors.add(2)
n //= 2
# Check for odd factors
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
factors.add(i)
n //= i
# If n is a prime greater than 2
if n > 2:
factors.add(n)
return factors
def find_longest_subsequence_with_gcd_gt_1(numbers):
factor_to_numbers = defaultdict(list)
for num in numbers:
if num <= 1:
continue
prime_factors = get_prime_factors(num)
for factor in prime_factors:
factor_to_numbers[factor].append(num)
# Find the factor that appears in the most numbers
max_length = 0
for factor, nums in factor_to_numbers.items():
if len(nums) > max_length:
max_length = len(nums)
return max_length
def solve():
host = "play.scriptsorcerers.xyz"
port = 10108
sh = remote(host, port)
all_data = b""
# to avoid bytestream break from too much input
numbers = []
total_numbers = 0
pbar = tqdm(desc="Receiving numbers", unit="num")
while True:
chunk = sh.recv(timeout=1)
if not chunk:
break
all_data += chunk
# decode and extract numbers from the current chunk
chunk_decoded = chunk.decode(errors="ignore")
chunk_numbers = [int(x) for x in chunk_decoded.split() if x.isdigit()]
numbers.extend(chunk_numbers)
total_numbers += len(chunk_numbers)
pbar.update(len(chunk_numbers))
if b"\n" in chunk and len(all_data) > 1000:
break
pbar.close()
all_data = all_data.decode(errors="ignore")
all_numbers = [int(x) for x in all_data.split() if x.isdigit()]
result = find_longest_subsequence_with_gcd_gt_1(all_numbers)
sh.sendline(str(result).encode())
flag = sh.recvline(timeout=2)
print(flag.decode(errors="ignore").strip())
solve()

scriptCTF{7H3_m0r3_f4C70r5_7h3_b3773r_55de2cb13555}
Windows To Infinity
windows and windows and windows and windows and windows and winflag???? (if there are any questions about input/output format, plz open a ticket)
mport random
import subprocess
n = 1000000
window_size = n / 2
"""
You will receive {n} numbers.
Every round, you will need to calculate a specific value for every window.
You will be doing the calculations on the same {n} numbers every round.
For example, in this round, you will need to find the sum of every window.
Sample testcase for Round 1 if n = 10
Input:
1 6 2 8 7 6 2 8 3 8
Output:
24 29 25 31 26 27
"""
a = []
for i in range(n):
a.append(str(random.randint(0, 100000)))
print(a[i], end=' ')
print()
proc = subprocess.Popen(['./solve'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
proc.stdin.write(' '.join(a) + '\n')
proc.stdin.flush()
def round(roundnumber, roundname):
print(f"Round {roundnumber}: {roundname}!")
ur_output = list(map(int, input().split()))
correct_output = list(map(int, proc.stdout.readline().split()))
if ur_output != correct_output:
print('uh oh')
exit(1)
round(1, "Sums")
round(2, "Xors")
round(3, "Means") # Note: means are rounded down
round(4, "Median") # Note: medians are calculated using a[floor(n / 2)]
round(5, "Modes") # Note: if there is a tie, print the mode with the largest value
round(6, "Mex (minimum excluded)") # examples: mex(5, 4, 2, 0) = 1, mex(4, 1, 2, 0) = 3, mex(5, 4, 2, 1) = 0
round(7, "# of Distinct Numbers")
round(8, "Sum of pairwise GCD") # If bounds of the window are [l, r], find the sum of gcd(a[i], a[j]) for every i, j where l <= i < j <= r,
print(open('flag.txt', 'r').readline())
Last updated