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
| import numpy as np
def inv(a,p): if(a == 1): return 1 return (p-p//a)*inv(p%a,p)%p
def trip(A, b, p = 2):
n = len(A) px = list(range(n)) for i in range(n): j,k = i,i while(k<n): j=i while(j<n and A[j,k]==0): j+=1 if(j != n): break k+=1 if(k == n): return i,px if(i!=k): px[i],px[k] = px[k],px[i] A[:,[i,k]] = A[:,[k,i]] if(j!=i): b[[i,j]] = b[[j,i]] A[[i,j]] = A[[j,i]] for j in range(i+1,n): ratio = A[j,i]*inv(A[i,i],p)%p b[j] = (b[j]-b[i]*ratio)%p A[j,i:n] = (A[j,i:n]-A[i,i:n]*ratio)%p return n,px
def trisolvep(A, b, p=2): ans = b.copy() n = len(A) for i in range(n-1,-1,-1): ans[i] = ans[i]*inv(A[i,i],p)%p ans[:i] = (ans[:i] - A[:i,i]*ans[i])%p return ans
def solvep(A, b, p=2):
n = len(A) for i in range(n): for j in range(n): A[i,j]%=p b[i]%=p r,px = trip(A,b,p) py = list(range(n)) for i in range(n): py[px[i]] = i if(r == n): return trisolvep(A,b,p) for i in range(r,n): if(b[i,0] != 0): return None ans = np.matrix(np.zeros([n,n-r+1], dtype=np.int)) ans[:r,0] = trisolvep(A[:r,:r],b[:r]) for i in range(r,n): ans[:r,i-r+1] = trisolvep(A[:r,:r],(-A[:r,i])%p) ans[i,i-r+1] = 1 return ans[py]
def lighton(x):
n = x.size ans = np.matrix(np.ones([n,n]),dtype = np.int) ans[0,:] = x for i in range(1,n): for j in range(n): if(i-2>=0): ans[i,j] ^= ans[i-2,j] ans[i,j] ^= ans[i-1,j] if(j-1>=0): ans[i,j] ^= ans[i-1,j-1] if(j+1<n): ans[i,j] ^= ans[i-1,j+1] return ans
def light(n):
if(n == 1): return [np.matrix('1')] x = np.matrix(np.zeros([n,n+1]),dtype = np.int) y = np.matrix(np.zeros([n,n+1]),dtype = np.int) for i in range(n): x[i,i] = 1 y[i,-1] = 1 for i in range(n): y[i,:] -= x[i,:] if(i-1>=0): y[i,:] -= x[i-1,:] if(i+1<n): y[i,:] -= x[i+1,:] for i in range(2,n): last = np.matrix(np.zeros([n,n+1]),dtype = np.int) for i in range(n): last[i,-1] = 1 for i in range(n): last[i,:] -= x[i,:] last[i,:] -= y[i,:] if(i-1>=0): last[i,:] -= y[i-1,:] if(i+1<n): last[i,:] -= y[i+1,:] x = y y = last A = np.matrix(np.zeros([n,2*n]),dtype = np.int) for i in range(n): A[i,i] = A[i,i+n]=1 if(i-1>=0): A[i,i-1+n] = 1 if(i+1<n): A[i,i+1+n] = 1 A = A*np.vstack((x,y)) b = np.matrix(np.ones([n,1]),dtype = np.int) ans = np.matrix(np.zeros([n,n]),dtype = np.int) x = solvep(A[:,:n],b - A[:,-1]).T cnt = 2**(len(x)-1) ans = [] for i in range(cnt): x0 = np.copy(x[0,:]) index = 0 while(i): index+=1 if(i&1): x0+=x[index,:] i>>=1 ans.append(lighton(x0&1)) return ans
while(1): n = int(input('输入n:')) m = light(n) print('方案数:'+str(len(m))) print(m)
|