-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhack.py
144 lines (135 loc) · 3.07 KB
/
hack.py
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
from frac import frac
from utils import d
import cvxpy as cp
from numpy import array
l = d+2
k = 100
def find_sol(a, b, c):
x = cp.Variable(l, integer=True)
obj = cp.Minimize(array(c)@x)
cons = [array(a)@x<=array(b),x>=1]
prob = cp.Problem(obj, cons)
prob.solve(solver=cp.CPLEX)
if prob.status == cp.OPTIMAL:
return int(x.value[0])
else:
return 0
def find_sols(public):
p = public['a']
n = len(p)
c = [1]
a = []
b = []
sols = []
for i in range(1,l):
c.append(0)
tmp = [0]*l
tmp[0] = p[i]
tmp[i] = -p[0]
a.append(tmp)
b.append(p[0]>>n-i)
tmp = [0]*l
tmp[0] = -p[i]
tmp[i] = p[0]
a.append(tmp)
b.append(p[i]>>n)
tmp = [0]*l
tmp[i] = 1
a.append(tmp)
b.append(p[i]-1)
tmp = [0]*l
tmp[0] = 1
a.append(tmp)
b.append(p[i]-1)
tmp = [0]*l
tmp[0] = -1
a.append(tmp)
b.append(-1)
for i in range(k):
x = find_sol(a,b,c)
if x <= 0 or x >= p[0]:
break
else:
sols.append(x)
b[-1] = -x-1
return sols
def to_small_segs1(public, s):
a = public['a']
n = len(a)
ret = []
for x in s:
t = [frac(x,a[0]), frac(x+1,a[0])]
for i in range(1, n):
l = (a[i]*x+a[0]-1)//a[0]
r = (a[i]*(x+1)+a[0]-1)//a[0]
for y in range(l,r):
t.append(frac(y,a[i]))
t.sort(reverse=False)
m = len(t)
for i in range(m-1):
if t[i]!=t[i+1]:
ret.append((t[i],t[i+1]))
print(ret)
return ret
def to_small_segs2(public, s):
a = public['a']
n = len(a)
ret = []
for p in s:
l,r = p
u = 0
v = 0
legal = True
for i in range(n):
d = l.a*a[i]//l.b
x = a[i]-u
y = d-v
t = frac(y,x)
if x>0 and t>l:
l = t
elif x<0 and t<r:
r = t
elif x==0 and y>=0:
legal = False
u += a[i]
v += d
v += 1
t = frac(v,u)
if t<r:
r = t
if legal and l<r:
ret.append((l,r))
return ret
def to_small_segs(public, s):
return to_small_segs2(public, to_small_segs1(public, s))
def simple_frac(a, b, c, d):
if a>=b:
t = a//b
r = simple_frac(a-b*t,b,c-d*t,d)
return frac(r.a+t*r.b,r.b)
if c>d:
return frac(1,1)
if a==0:
return frac(1,d//c+1)
r = simple_frac(d,c,b,a)
return frac(r.b,r.a)
def seg_to_frac(s):
l,r = s
assert l<r
return simple_frac(l.a, l.b, r.a, r.b)
def seg_to_fracs(s, x, y, d):
f = seg_to_frac(s)
ret = []
if f.b>=y:
return ret
if f.b>=x:
ret.append((f,d))
l,r = s
ret.extend(seg_to_fracs((l,f),x,y,d+1))
ret.extend(seg_to_fracs((f,r),x,y,d+1))
return ret
def segs_to_fracs(segs, x, y):
ret = []
for s in segs:
ret.extend(seg_to_fracs(s,x,y,0))
return ret