Download tasks/MATH1/solution/solver.py from ulamai/STEM-RL-Tasks: direct link, hf CLI and curl.
- Browser
- Download file 5.42 kB
-
https://huggingface.co/datasets/ulamai/STEM-RL-Tasks/resolve/main/tasks/MATH1/solution/solver.py
- Command line
-
hf download hf://datasets/ulamai/STEM-RL-Tasks/tasks/MATH1/solution/solver.py
-
curl -L -o solver.py https://huggingface.co/datasets/ulamai/STEM-RL-Tasks/resolve/main/tasks/MATH1/solution/solver.py
5.42 kB
| import json | |
| def solve(): | |
| p = 7 | |
| # f(x) = x^5 + 2x^3 + 3x + 1 | |
| poly = [1, 0, 2, 0, 3, 1] # x^5, x^4, x^3, x^2, x^1, x^0 | |
| def get_f(x, p_val): | |
| # x is an element of GF(p^k) represented as a tuple or int | |
| # But for p=7, k=1,2,3, we can just use a simple power-sum approach | |
| pass | |
| def count_points(k): | |
| q = p**k | |
| count = 0 | |
| if k == 1: | |
| for x in range(q): | |
| val = (x**5 + 2*x**3 + 3*x + 1) % p | |
| if val == 0: | |
| count += 1 | |
| elif pow(val, (p-1)//2, p) == 1: | |
| count += 2 | |
| elif k == 2: | |
| # GF(49) = Z_7[t] / (t^2 + 1) since 7 = 3 mod 4 | |
| for a in range(p): | |
| for b in range(p): | |
| # x = a + bt, x^2 = (a^2-b^2) + 2abt | |
| # Compute f(x) in GF(49) | |
| x = complex_val(a, b, p) | |
| val = f_eval(x, p) | |
| if val == (0, 0): | |
| count += 1 | |
| elif is_square_gf49(val, p): | |
| count += 2 | |
| elif k == 3: | |
| # GF(343) = Z_7[t] / (t^3 + t + 1) | |
| for a in range(p): | |
| for b in range(p): | |
| for c in range(p): | |
| x = (a, b, c) | |
| val = f_eval_gf343(x, p) | |
| if val == (0, 0, 0): | |
| count += 1 | |
| elif is_square_gf343(val, p): | |
| count += 2 | |
| return count + 1 # Point at infinity | |
| def complex_val(a, b, p): return (a % p, b % p) | |
| def f_eval(x, p): | |
| # x = (a, b) -> a + bt. t^2 = -1 | |
| res = (0, 0) | |
| curr = (1, 0) | |
| coeffs = [1, 3, 0, 2, 0, 1] # 1, x, x^2, x^3, x^4, x^5 | |
| for c in coeffs: | |
| res = ((res[0] + c * curr[0]) % p, (res[1] + c * curr[1]) % p) | |
| # multiply curr by x | |
| curr = ((curr[0]*x[0] - curr[1]*x[1]) % p, (curr[0]*x[1] + curr[1]*x[0]) % p) | |
| return res | |
| def is_square_gf49(val, p): | |
| # val^{(q-1)/2} == 1. q=49, exp=24 | |
| res = (1, 0) | |
| base = val | |
| exp = 24 | |
| while exp > 0: | |
| if exp % 2 == 1: | |
| res = ((res[0]*base[0] - res[1]*base[1]) % p, (res[0]*base[1] + res[1]*base[0]) % p) | |
| new_base = ((base[0]*base[0] - base[1]*base[1]) % p, (2*base[0]*base[1]) % p) | |
| base = new_base | |
| exp //= 2 | |
| return res == (1, 0) | |
| def f_eval_gf343(x, p): | |
| # x = (a, b, c) -> a + bt + ct^2. t^3 = -t - 1 | |
| res = (0, 0, 0) | |
| curr = (1, 0, 0) | |
| coeffs = [1, 3, 0, 2, 0, 1] | |
| for c in coeffs: | |
| res = ((res[0] + c * curr[0]) % p, (res[1] + c * curr[1]) % p, (res[2] + c * curr[2]) % p) | |
| # multiply curr by x | |
| # (a0, a1, a2)*(b0, b1, b2) | |
| a, b = curr, x | |
| w = [0]*5 | |
| for i in range(3): | |
| for j in range(3): | |
| w[i+j] = (w[i+j] + a[i]*b[j]) % p | |
| # t^3 = -t-1, t^4 = -t^2-t | |
| w2 = (w[2] - w[4]) % p | |
| w1 = (w[1] - w[3] - w[4]) % p | |
| w0 = (w[0] - w[3]) % p | |
| curr = (w0, w1, w2) | |
| return res | |
| def is_square_gf343(val, p): | |
| # q=343, exp=171 | |
| res = (1, 0, 0) | |
| base = val | |
| exp = 171 | |
| while exp > 0: | |
| if exp % 2 == 1: | |
| a, b = res, base | |
| w = [0]*5 | |
| for i in range(3): | |
| for j in range(3): w[i+j] = (w[i+j] + a[i]*b[j]) % p | |
| res = ((w[0]-w[3])%p, (w[1]-w[3]-w[4])%p, (w[2]-w[4])%p) | |
| a = base | |
| w = [0]*5 | |
| for i in range(3): | |
| for j in range(3): w[i+j] = (w[i+j] + a[i]*a[j]) % p | |
| base = ((w[0]-w[3])%p, (w[1]-w[3]-w[4])%p, (w[2]-w[4])%p) | |
| exp //= 2 | |
| return res == (1, 0, 0) | |
| n1 = count_points(1) | |
| n2 = count_points(2) | |
| n3 = count_points(3) | |
| a1 = p + 1 - n1 | |
| a2 = p**2 + 1 - n2 | |
| a3 = p**3 + 1 - n3 | |
| # P(T) = T^4 + c1 T^3 + c2 T^2 + c3 T + c4 | |
| # Newton: s1 = -c1, s2 = c1*s1 - 2*c2, s3 = c1*s2 - c2*s1 + 3*c3 | |
| c1 = -a1 | |
| c2 = (c1*(-a1) - a2) // 2 | |
| c3 = (a3 - c1*a2 + c2*a1) // -3 | |
| c4 = p**2 | |
| # Hasse-Witt | |
| # f(x) = x^5 + 2x^3 + 3x + 1. (p-1)/2 = 3. | |
| # f(x)^3 = (x^5 + 2x^3 + 3x + 1)^3 | |
| f_coeffs = {5:1, 3:2, 1:3, 0:1} | |
| f3 = {} | |
| for i1, v1 in f_coeffs.items(): | |
| for i2, v2 in f_coeffs.items(): | |
| for i3, v3 in f_coeffs.items(): | |
| deg = i1+i2+i3 | |
| f3[deg] = (f3.get(deg, 0) + v1*v2*v3) % p | |
| # M = [[coeff(x^6), coeff(x^5)], [coeff(x^13), coeff(x^12)]] | |
| m11, m12 = f3.get(6, 0), f3.get(5, 0) | |
| m21, m22 = f3.get(13, 0), f3.get(12, 0) | |
| hw_det = (m11*m22 - m12*m21) % p | |
| # p-rank: rank of HW matrix | |
| # Since it's 2x2, rank is 2 if det != 0, 1 if det=0 but M != 0, 0 if M=0. | |
| if hw_det != 0: p_rank = 2 | |
| elif any(v != 0 for v in [m11, m12, m21, m22]): p_rank = 1 | |
| else: p_rank = 0 | |
| res = { | |
| "problem_id": "MATH1", | |
| "frobenius_traces": [int(a1), int(a2), int(a3)], | |
| "char_poly_coeffs": [1, int(c1), int(c2), int(c3), int(c4)], | |
| "newton_slopes": ["1/2", "1/2", "1/2", "1/2"], | |
| "hasse_witt_det": int(hw_det), | |
| "jacobian_p_rank": int(p_rank) | |
| } | |
| print(json.dumps(res)) | |
| solve() | |