Skip to content

Instantly share code, notes, and snippets.

@emaadmanzoor
Created September 9, 2013 13:32
Show Gist options
  • Save emaadmanzoor/6495628 to your computer and use it in GitHub Desktop.
Save emaadmanzoor/6495628 to your computer and use it in GitHub Desktop.
Frievald's Algorithm
import random
import operator
t = int(raw_input())
randint = random.randint
def deterministic(a,b,c,n):
no = 0
for p in xrange(n):
for q in xrange(n):
ands = 0
for r in xrange(n):
if (a[p][r] and b[r][q]):
ands = 1
break
if c[p][q] != ands:
no = 1
break
if no:
break
if no:
return False
else:
return True
for i in range(t):
raw_input()
n = int(raw_input())
a = [0] * n
b = [[0] * n]*n
c = [0] * n
for j in range(n):
a[j] = map(int, raw_input().split())
raw_input()
for j in range(n):
b[j] = map(int, raw_input().split())
raw_input()
for j in range(n):
c[j] = map(int, raw_input().split())
trials = 0
numtrials = 4
for x in range(numtrials):
v = [randint(0,1) for x in range(n)]
Br = [reduce(operator.add, map(operator.mul,r,v)) for r in b]
ABr = [reduce(operator.add, map(operator.mul,r,Br)) for r in a]
Cr = [reduce(operator.add, map(operator.mul,r,v)) for r in c]
if ABr == Cr:
trials = trials + 1
if trials == numtrials:
print "yes"
elif deterministic(a,b,c,n):
print "yes"
else:
print "no"
@elliotmtx
Copy link

Please say the input format how to provide, it will be very helpful
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment