File size: 5,420 Bytes
6395f35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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()