实验指导
仅供参考。
Task 1
逃课写法:
1 2 3 4 5
| from math import gcd
def inverse(x, n): return pow(x, -1, n)
|
造轮子写法:
1 2 3 4 5 6 7 8 9 10 11 12 13
| def exgcd(x, y): if y: a, b, r = exgcd(y, x % y) return b, a - (x // y) * b, r return 1, 0, x
def gcd(x, y): return exgcd(x, y)[2]
def inverse(x, n): return exgcd(x, n)[0] % n
|
Task 2
只写了前两个比较好写的,BabyDLP 因为不想学数学了所以摆了(orz
第一个就是 RSA,e=230+3,可以直接用 Sage 的 factor()
解决
ccpc.py1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from sage.all import *
e = (1 << 30) + 3
for i in range(int(input())): n, c = map(int, input().split())
(p, _), (q, _) = factor(n)
assert p * q == n
m = (p - 1) * (q - 1) assert gcd(m, e) == 1
d = pow(e, -1, m) x = pow(c, d, n) print(f'Case {i + 1}: {x}')
|
第二个是较简单的离散对数,可以直接用 discrete_log()
一把梭
hdu.py1 2 3 4 5 6 7 8 9 10 11 12
| from sage.all import *
for i in range(int(input())): p, a, b = map(int, input().split())
G = GF(p) try: print(discrete_log(G(b), G(a))) except ValueError: print(-1)
|