2018-06-07 11:15:18 +02:00
|
|
|
import sys
|
|
|
|
import ast
|
|
|
|
|
|
|
|
def modulo(x,n):
|
|
|
|
for i in range(len(x)):
|
|
|
|
x[i] = x[i] % n
|
|
|
|
return x
|
|
|
|
|
|
|
|
def iloczyn(f,g,n):
|
|
|
|
wynik = [0]*(len(f)+len(g)-1)
|
|
|
|
for i in range(len(f)):
|
|
|
|
for j in range(len(g)):
|
|
|
|
wynik[i+j] += f[i]*g[j]
|
|
|
|
return modulo(wynik,n)
|
|
|
|
|
|
|
|
def liczmod(a,x,n):
|
|
|
|
for i in range(n):
|
|
|
|
if( (a*i) % n == x):
|
|
|
|
return(i)
|
|
|
|
return(-1)
|
|
|
|
|
|
|
|
def sumujmod(a,b,n):
|
|
|
|
if(len(a) < len(b)):
|
|
|
|
wynik = b
|
|
|
|
for i in range(len(a)):
|
|
|
|
wynik[i] += a[i]
|
|
|
|
else:
|
|
|
|
wynik = a
|
|
|
|
for i in range(len(b)):
|
|
|
|
wynik[i] += b[i]
|
|
|
|
return modulo(wynik,n)
|
|
|
|
|
|
|
|
def redukcjazer(x):
|
|
|
|
while x and x[-1] is 0:
|
|
|
|
x.pop()
|
|
|
|
return(x)
|
|
|
|
|
|
|
|
def klasaReszt(f,g,n):
|
|
|
|
wynik = f
|
|
|
|
mnoznik = [0] * (len(f)-len(g)+2)
|
|
|
|
for i in range(len(f)):
|
|
|
|
if(liczmod(g[len(g)-1],wynik[len(f)-1-i],n) == -1):
|
|
|
|
return("DivisionError")
|
|
|
|
if(i > len(f)-len(g)):
|
|
|
|
return redukcjazer(modulo(wynik,n))
|
|
|
|
else:
|
|
|
|
mnoznik.pop()
|
|
|
|
mnoznik.pop()
|
|
|
|
mnoznik.append(-1*liczmod(g[len(g)-1],wynik[len(f)-1-i],n))
|
|
|
|
wynik = sumujmod(wynik, iloczyn(g, mnoznik ,n) ,n)
|
|
|
|
|
|
|
|
def nwd(a,b,n):
|
|
|
|
while(b != []):
|
|
|
|
if(klasaReszt(a,b,n)):
|
|
|
|
return "DivisionError"
|
|
|
|
c = klasaReszt(a,b,n)
|
|
|
|
a = b
|
|
|
|
b = c
|
|
|
|
return modulo(a,n)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
n = int(sys.argv[1])
|
|
|
|
f = ast.literal_eval(sys.argv[2])
|
|
|
|
g = ast.literal_eval(sys.argv[3])
|
|
|
|
|
|
|
|
print(iloczyn(f,g,n))
|
|
|
|
print(klasaReszt(f,g,n))
|
|
|
|
print(nwd(f,g,n))
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|