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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// Asymptote module implementing rational arithmetic.
int gcd(int m, int n)
{
if(m < n) {
int temp=m;
m=n;
n=temp;
}
while(n != 0) {
int r=m % n;
m=n;
n=r;
}
return m;
}
int lcm(int m, int n)
{
return m#gcd(m,n)*n;
}
struct rational {
int p=0,q=1;
void reduce() {
int d=gcd(p,q);
if(abs(d) > 1) {
p #= d;
q #= d;
}
if(q <= 0) {
if(q == 0) abort("Division by zero");
p=-p;
q=-q;
}
}
void operator init(int p=0, int q=1, bool reduce=true) {
this.p=p;
this.q=q;
if(reduce) reduce();
}
}
rational operator cast(int p) {
return rational(p,false);
}
rational[] operator cast(int[] a) {
return sequence(new rational(int i) {return a[i];},a.length);
}
rational[][] operator cast(int[][] a) {
return sequence(new rational[](int i) {return a[i];},a.length);
}
real operator ecast(rational r) {
return r.p/r.q;
}
rational operator -(rational r)
{
return rational(-r.p,r.q,false);
}
rational operator +(rational r, rational s)
{
return rational(r.p*s.q+s.p*r.q,r.q*s.q);
}
rational operator -(rational r, rational s)
{
return rational(r.p*s.q-s.p*r.q,r.q*s.q);
}
rational operator *(rational r, rational s)
{
return rational(r.p*s.p,r.q*s.q);
}
rational operator /(rational r, rational s)
{
return rational(r.p*s.q,r.q*s.p);
}
bool operator ==(rational r, rational s)
{
return r.p == s.p && r.q == s.q;
}
bool operator !=(rational r, rational s)
{
return r.p != s.p || r.q != s.q;
}
bool operator <(rational r, rational s)
{
return r.p*s.q-s.p*r.q < 0;
}
bool operator >(rational r, rational s)
{
return r.p*s.q-s.p*r.q > 0;
}
bool operator <=(rational r, rational s)
{
return r.p*s.q-s.p*r.q <= 0;
}
bool operator >=(rational r, rational s)
{
return r.p*s.q-s.p*r.q >= 0;
}
bool[] operator ==(rational[] r, rational s)
{
return sequence(new bool(int i) {return r[i] == s;},r.length);
}
bool operator ==(rational[] r, rational[] s)
{
if(r.length != s.length)
abort(" operation attempted on arrays of different lengths: "+
string(r.length)+" != "+string(s.length));
return all(sequence(new bool(int i) {return r[i] == s[i];},r.length));
}
bool operator ==(rational[][] r, rational[][] s)
{
if(r.length != s.length)
abort(" operation attempted on arrays of different lengths: "+
string(r.length)+" != "+string(s.length));
return all(sequence(new bool(int i) {return r[i] == s[i];},r.length));
}
bool[] operator <(rational[] r, rational s)
{
return sequence(new bool(int i) {return r[i] < s;},r.length);
}
bool[] operator >(rational[] r, rational s)
{
return sequence(new bool(int i) {return r[i] > s;},r.length);
}
bool[] operator <=(rational[] r, rational s)
{
return sequence(new bool(int i) {return r[i] <= s;},r.length);
}
bool[] operator >=(rational[] r, rational s)
{
return sequence(new bool(int i) {return r[i] >= s;},r.length);
}
rational min(rational a, rational b)
{
return a <= b ? a : b;
}
rational max(rational a, rational b)
{
return a >= b ? a : b;
}
string string(rational r)
{
return r.q == 1 ? string(r.p) : string(r.p)+"/"+string(r.q);
}
string texstring(rational r)
{
if(r.q == 1) return string(r.p);
string s;
if(r.p < 0) s="-";
return s+"\frac{"+string(abs(r.p))+"}{"+string(r.q)+"}";
}
void write(file fout, string s="", rational r, suffix suffix=none)
{
write(fout,s+string(r),suffix);
}
void write(string s="", rational r, suffix suffix=endl)
{
write(stdout,s,r,suffix);
}
void write(file fout=stdout, string s="", rational[] a, suffix suffix=none)
{
if(s != "")
write(fout,s,endl);
for(int i=0; i < a.length; ++i) {
write(fout,i,none);
write(fout,':\t',a[i],endl);
}
write(fout,suffix);
}
void write(file fout=stdout, string s="", rational[][] a, suffix suffix=none)
{
if(s != "")
write(fout,s);
for(int i=0; i < a.length; ++i) {
rational[] ai=a[i];
for(int j=0; j < ai.length; ++j) {
write(fout,ai[j],tab);
}
write(fout,endl);
}
write(fout,suffix);
}
bool rectangular(rational[][] m)
{
int n=m.length;
if(n > 0) {
int m0=m[0].length;
for(int i=1; i < n; ++i)
if(m[i].length != m0) return false;
}
return true;
}
rational sum(rational[] a)
{
rational sum;
for(rational r:a)
sum += r;
return sum;
}
rational max(rational[] a)
{
rational M=a[0];
for(rational r:a)
M=max(M,r);
return M;
}
rational abs(rational r)
{
return rational(abs(r.p),r.q,false);
}
rational[] operator -(rational[] r)
{
return sequence(new rational(int i) {return -r[i];},r.length);
}
rational[][] rationalidentity(int n)
{
return sequence(new rational[](int i) {return sequence(new rational(int j) {return j == i ? 1 : 0;},n);},n);
}
/*
rational r=rational(1,3)+rational(1,4);
write(r == rational(1,12));
write(r);
real x=r;
write(x);
rational r=3;
write(r);
write(gcd(-8,12));
write(rational(-36,-14));
int[][] a=new int[][] {{1,2},{3,4}};
rational[][] r=a;
write(r);
*/
|