forked from joseprio/codejamide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.html
1917 lines (1709 loc) · 50.1 KB
/
runner.html
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<!--
Hello there!!
So, checking my code jam solution? Good!
This is not the real page that I used to develop the code, but a "running
framework" that is used to run the same code.
The reasons why I'm not uploading the real one:
- This one is compatible with more browsers (in theory, it should be compatible
even with IE6).
- Just takes a single file.
- So much more smaller, so faster to upload.
Of course, this page will render the same solution, and contains the same
libraries I developed.
Get the full version at http://github.com/joseprio/codejamide
-->
<title>Runner</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
overflow: hidden;
font-family: Verdana, Arial, sans-serif;
}
#editorDiv form {
position: relative;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
height: 100%;
}
#editorDiv form textarea {
width: 100%;
height: 100%;
}
#inputDiv, #outputDiv, #consoleDiv, #commandsDiv, #editorDiv {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
width: 50%;
height: 50%;
padding: 10px;
float: left;
}
#ioDiv, #editorDiv {
width: 50%;
height: 100%;
float: left;
}
#consoleDiv {
width: 70%;
}
#commandsDiv {
width: 30%;
}
#commandsDiv input {
width: 100%;
margin-bottom: 5px;
}
#inputDiv form, #outputDiv form, #consoleDiv form, #editorDiv form {
height: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
padding-bottom: 20px;
}
#inputDiv textarea, #outputDiv textarea, #consoleDiv textarea {
width: 100%;
height: 100%;
}
.title {
font-weight: bold;
}
</style>
</head>
<body>
<div id="ioDiv">
<div id="inputDiv">
<div class="title">Input</div>
<form>
<input type="file" id="inputfile" accept="*" style="display:none" onchange="handleInputFiles(this.files)">
<textarea id="input" name="input"></textarea></form>
</div>
<div id="outputDiv">
<div class="title">Output</div>
<form><textarea id="output" name="output" readonly></textarea></form>
</div>
<div id="consoleDiv">
<div class="title">Console</div>
<form><textarea id="console" name="console" readonly></textarea></form>
</div>
<div id="commandsDiv">
<input type="button" value="Run" onclick="runProgram();"/>
</div>
</div>
<div id="editorDiv">
<div class="title">Code Editor</div>
<form><textarea id="code" name="code"></textarea>
</form>
</div>
<script>
var currentInput = [];
function readLine() {
return currentInput.shift();
}
function readInt() {
return parseInt(readLine());
}
function readStrings() {
return readLine().split(' ');
var returnValue = [];
for (var c=0;c<parts.length;c++) {
returnValue.push(parseInt(parts[c]));
}
return returnValue;
}
function readInts() {
var parts = readStrings();
var returnValue = [];
for (var c=0;c<parts.length;c++) {
returnValue.push(parseInt(parts[c]));
}
return returnValue;
}
function readFloats() {
var parts = readStrings();
var returnValue = [];
for (var c=0;c<parts.length;c++) {
returnValue.push(parseFloat(parts[c]));
}
return returnValue;
}
function smartRead(firstIsCount) {
var parts = readStrings();
var total = parts.length;
if (typeof firstIsCount == "undefined") {
var firstNumber = parseInt(parts[0]);
if ((total > 1) &&
(!isNaN(firstNumber))&&
((total - 1)%firstNumber == 0)
) {
isFirstCount = true;
}
}
if (isFirstCount) {
total = parseInt(parts.shift());
}
var perGroup = Math.floor(parts.length / total);
for (var c=0;c<total;c++) {
var resultPart;
if (perGroup > 1) {
resultPart = [];
}
for (var d=0;d<perGroup;d++) {
var newPart = parts[c*perGroup+d];
if (newPart.indexOf('.') > -1) {
if (!isNaN(parseFloat(newPart))) {
newPart = parseFloat(newPart);
}
} else {
if (!isNaN(parseInt(newPart))) {
newPart = parseInt(newPart);
}
}
if (perGroup > 1) {
resultPart.push(newPart);
} else {
parts[c*perGroup+d] = newPart;
}
}
if (perGroup > 1) {
parts[c] = resultPart;
}
}
parts.length = total;
return parts;
}
var currentOutputLine = "";
function print() {
for (var c=0;c<arguments.length;c++) {
if (c > 0) {
currentOutputLine += " ";
}
if (typeof(arguments[c]) == "object") {
if (Arrays.isArray(arguments[c])) {
for (var d=0;d<arguments[c].length;d++) {
if (d > 0) {
currentOutputLine += " ";
}
currentOutputLine += arguments[c][d];
}
} else {
currentOutputLine += arguments[c];
}
} else {
currentOutputLine += arguments[c];
}
}
}
function println() {
print.apply(null, arguments);
currentOutputLine += "\n";
}
function getOutputLine() {
return currentOutputLine;
}
function resetOutputLine() {
currentOutputLine = '';
}
function log() {
if (!showLogs) return;
var currentDate = new Date();
var hours = '' + currentDate.getHours();
if (hours.length < 2) hours = '0' + hours;
var minutes = '' + currentDate.getMinutes();
if (minutes.length < 2) minutes = '0' + minutes;
var seconds = '' + currentDate.getSeconds();
if (seconds.length < 2) seconds = '0' + seconds;
var millis = '' + currentDate.getMilliseconds();
while (millis.length < 3) millis = '0' + millis;
var message = hours + ':' + minutes + ':' + seconds + '.' + millis;
message += '/' + currentIteration;
for (var c=0;c<arguments.length;c++) {
message += ' - ';
var currentArg = arguments[c];
if (typeof currentArg == "string") {
message += currentArg;
} else {
message += JSON.stringify(currentArg);
}
}
message += "\n------------\n";
var console = document.getElementById('console');
message += console.value;
if (message.length > 2000) {
message.length = 2000;
}
console.value = message;
}
var cache = {};
function nullifyCache() { cache = {}; }
var currentIteration = 0;
var showLogs = true;
function runProgram() {
var numCases = -1;
currentInput = document.getElementById('input').value.split("\n");
try {
eval('var program = function() { ' + document.getElementById('code').value + ' }');
} catch (e) {
log('Exception while evaluating program', e);
return;
}
var totalCases = readInt();
if ((numCases < 1)|| (numCases > totalCases)) {
numCases = totalCases;
}
nullifyCache();
var realProgramOutput = "";
for (var c=1;c<=numCases;c++) {
currentIteration = c;
resetOutputLine();
var programResult;
try {
programResult = program();
} catch (e) {
log('Exception in iteration ' + c + ' while running program', e);
}
if (typeof programResult != "undefined") {
print(programResult);
}
realProgramOutput += 'Case #' + c + ': ' + getOutputLine() + "\n";
}
document.getElementById('output').value = realProgramOutput;
}
function BinaryHeap(scoreFunction){
this.content = [];
this.scoreFunction = scoreFunction;
}
BinaryHeap.prototype = {
push: function(element) {
// Add the new element to the end of the array.
this.content.push(element);
// Allow it to bubble up.
this.bubbleUp(this.content.length - 1);
},
pop: function() {
// Store the first element so we can return it later.
var result = this.content[0];
// Get the element at the end of the array.
var end = this.content.pop();
// If there are any elements left, put the end element at the
// start, and let it sink down.
if (this.content.length > 0) {
this.content[0] = end;
this.sinkDown(0);
}
return result;
},
remove: function(node) {
var len = this.content.length;
// To remove a value, we must search through the array to find
// it.
for (var i = 0; i < len; i++) {
if (this.content[i] == node) {
// When it is found, the process seen in 'pop' is repeated
// to fill up the hole.
var end = this.content.pop();
if (i != len - 1) {
this.content[i] = end;
if (this.scoreFunction(end) < this.scoreFunction(node))
this.bubbleUp(i);
else
this.sinkDown(i);
}
return;
}
}
throw new Error("Node not found.");
},
size: function() {
return this.content.length;
},
bubbleUp: function(n) {
// Fetch the element that has to be moved.
var element = this.content[n];
// When at 0, an element can not go up any further.
while (n > 0) {
// Compute the parent element's index, and fetch it.
var parentN = Math.floor((n + 1) / 2) - 1,
parent = this.content[parentN];
// Swap the elements if the parent is greater.
if (this.scoreFunction(element) < this.scoreFunction(parent)) {
this.content[parentN] = element;
this.content[n] = parent;
// Update 'n' to continue at the new position.
n = parentN;
}
// Found a parent that is less, no need to move it further.
else {
break;
}
}
},
sinkDown: function(n) {
// Look up the target element and its score.
var length = this.content.length,
element = this.content[n],
elemScore = this.scoreFunction(element);
while(true) {
// Compute the indices of the child elements.
var child2N = (n + 1) * 2, child1N = child2N - 1;
// This is used to store the new position of the element,
// if any.
var swap = null;
// If the first child exists (is inside the array)...
if (child1N < length) {
// Look it up and compute its score.
var child1 = this.content[child1N],
child1Score = this.scoreFunction(child1);
// If the score is less than our element's, we need to swap.
if (child1Score < elemScore)
swap = child1N;
}
// Do the same checks for the other child.
if (child2N < length) {
var child2 = this.content[child2N],
child2Score = this.scoreFunction(child2);
if (child2Score < (swap == null ? elemScore : child1Score))
swap = child2N;
}
// If the element needs to be moved, swap it, and continue.
if (swap != null) {
this.content[n] = this.content[swap];
this.content[swap] = element;
n = swap;
}
// Otherwise, we are done.
else {
break;
}
}
}
};
var Break = {toString: function() {return "Break";}};
function forEach(array, action) {
try {
for (var i = 0; i < array.length; i++) {
if (action(array[i], i, array.length) === false);
}
}
catch (exception) {
if (exception != Break)
throw exception;
}
}
/*!
* Arrays Libary v1.0
* http://joseprio.com/
*
* Copyright 2012, Josep del Rio
* Licensed under the MIT license.
*
*
* Date: Mon Apr 15 12:23:00 2012 +0100
*
* New functions:
*
* Arrays.create(m, n, ...)
* Creates a new multidimensional array of dimensions m*n*...
*
* Arrays.isArray(a)
* Returns true if a is an array, false otherwise
*
* Java equivalent function support:
*
* static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)
* static int binarySearch(Object[] a, Object key)
* static boolean equals(Object[] a, Object[] a2)
* static void fill(Object[] a, int fromIndex, int toIndex, Object val)
* static void fill(Object[] a, Object val)
* static void sort(Object[] a)
* static void sort(Object[] a, Comparator c)
* static void sort(Object[] a, int fromIndex, int toIndex)
* static void sort(Object[] a, int fromIndex, int toIndex, Comparator c)
* static String toString(Object[] a)
* static boolean deepEquals(Object[] a1, Object[] a2)
* static String deepToString(Object[] a)
* static Object[] copyOf(Object[] original, int newLength)
* static Object[] copyOfRange(Object[] original, int from, int to)
*
* Unimplemented:
* static int binarySearch(Object[] a, Object key, Comparator c)
* static int hashCode(Object[] a)
* static int deepHashCode(Object[] a)
* static List asList(Object[] a)
*
*
*/
var Arrays = {
sort: function() {
var target = arguments[0];
var sorter = null;
if (arguments.length == 2) {
sorter = arguments[1];
} else if (arguments.length == 4) {
sorter = arguments[3];
} else {
sorter = function(a, b) { if (a < b) return -1; if (a > b) return 1; return 0 };
}
if (arguments.length >= 3) {
// We have fromIndex and toIndex
var subtarget = Arrays.copyOfRange(target, arguments[1], arguments[2]);
subtarget.sort(sorter);
for (var c=arguments[1];c<=arguments[2];c++) {
target[c] = subtarget[c-arguments[1]];
}
} else {
target.sort(sorter);
}
},
create: function() {
// Create a multidimensional array
var total = arguments[0];
var result = new Array(total);
if (arguments.length > 1) {
var newArgs = [];
for (var c=1;c<arguments.length;c++) {
newArgs[c-1] = arguments[c];
}
for (var c=0;c<total;c++) {
result[c] = Arrays.create.apply(null, newArgs);
}
}
return result;
},
fill: function(target, value) {
// We fill the array with certain value
// If it's a multidimensional array, we fill them too
var fromIndex = 0,
toIndex = target.length-1,
valueIsFunction = (typeof value == "function"),
valueIsArray = Arrays.isArray(value);
// Support fromIndex/toIndex
if (arguments.length > 2) {
fromIndex = arguments[1];
toIndex = arguments[2];
value = arguments[3];
}
for (var c=fromIndex;c<=toIndex;c++) {
if (Arrays.isArray(target[c])) {
Arrays.fill(target[c], value);
} else {
if (valueIsFunction) {
target[c] = value.call(target, c);
} else if (valueIsArray) {
// If value is an array, create a copy to avoid problems
target[c] = Arrays.copyOf(value);
} else {
target[c] = value;
}
}
}
},
toString: function(target) {
return "[" + target.join(", ") + "]";
},
// TODO: handle arrays that contain themselves; when detected, use "[...]"
deepToString: function(target) {
var output = "[";
for (var c=0;c<target.length;c++) {
if (c > 0) {
output += ", ";
}
if (Arrays.isArray(target[c])) {
output += Arrays.deepToString(target[c]);
} else {
output += target[c];
}
}
output += "]";
return output;
},
isArray: Array.isArray || function(a) {
// Use Array.isArray if present; if not, we need to check the string value
return (Object.prototype.toString.call(a) === '[object Array]');
},
binarySearch: function(a, fromIndex, toIndex, key) {
// Binary search can be called in two ways: with from/to indexes or without
if (arguments.length < 4) {
key = fromIndex;
fromIndex = 0;
toIndex = a.length-1;
}
if (toIndex < fromIndex) {
// Not found
}
var pos = Math.floor((toIndex + fromIndex) / 2);
if (a[pos] === key) {
return pos;
} else if (fromIndex >= toIndex) {
// Failed to find it
return -Math.abs(toIndex)-1;
} else if (a[pos] > key) {
return Arrays.binarySearch(a, fromIndex, pos-1, key);
} else {
return Arrays.binarySearch(a, pos+1, toIndex, key);
}
},
copyOf: function(original, newLength) {
var newArray;
// copyOf can be called with newLength or without, so check for it
if (typeof newLength == "undefined") {
newLength = original.length;
}
if (newLength < original.length) {
newArray = original.slice(0, newLength);
} else {
newArray = original.slice(0);
newArray.length = newLength;
}
return newArray;
},
copyOfRange: function(original, from, to) {
return original.slice(from, to+1);
},
equals: function(a1, a2) {
if (a1.length != a2.length) return false;
var isEquals = true;
for (var c=0;(c<a2.length)&&isEquals;c++) {
isEquals = (a1[c] === a2[c]);
}
return isEquals;
},
// TODO: make sure we don't enter an infinite array contains itself
deepEquals: function(a1, a2) {
if (a1.length != a2.length) return false;
var isEquals = true;
for (var c=0;(c<a1.length)&&isEquals;c++) {
if (Arrays.isArray(a1[c]) && Arrays.isArray(a2[c])) {
isEquals = Arrays.deepEquals(a1[c], a2[c]);
} else {
isEquals = (a1[c] === a2[c]);
}
}
return isEquals;
}
};
// BigInteger
// Copyright (c) 2005 Tom Wu
// All Rights Reserved.
// See "LICENSE" for details.
// Basic JavaScript BN library - subset useful for RSA encryption.
// Bits per digit
var dbits;
// JavaScript engine analysis
var canary = 0xdeadbeefcafe;
var j_lm = ((canary&0xffffff)==0xefcafe);
// (public) Constructor
function BigInteger(a,b,c) {
if(a != null) {
if("number" == typeof a) {
if (!b && !c) {
this.fromString("" + a, 10);
} else{
this.fromNumber(a,b,c);
}
}
else if(b == null && "string" != typeof a) {
this.fromString(a,256);
} else {
if (typeof b == 'undefined') {
b=10;
}
this.fromString(a,b);
}
}
}
// return new, unset BigInteger
function nbi() { return new BigInteger(null); }
// am: Compute w_j += (x*this_i), propagate carries,
// c is initial carry, returns final carry.
// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
// We need to select the fastest one that works in this environment.
// am1: use a single mult and divide to get the high bits,
// max digit bits should be 26 because
// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
function am1(i,x,w,j,c,n) {
while(--n >= 0) {
var v = x*this[i++]+w[j]+c;
c = Math.floor(v/0x4000000);
w[j++] = v&0x3ffffff;
}
return c;
}
// am2 avoids a big mult-and-extract completely.
// Max digit bits should be <= 30 because we do bitwise ops
// on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
function am2(i,x,w,j,c,n) {
var xl = x&0x7fff, xh = x>>15;
while(--n >= 0) {
var l = this[i]&0x7fff;
var h = this[i++]>>15;
var m = xh*l+h*xl;
l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
w[j++] = l&0x3fffffff;
}
return c;
}
// Alternately, set max digit bits to 28 since some
// browsers slow down when dealing with 32-bit numbers.
function am3(i,x,w,j,c,n) {
var xl = x&0x3fff, xh = x>>14;
while(--n >= 0) {
var l = this[i]&0x3fff;
var h = this[i++]>>14;
var m = xh*l+h*xl;
l = xl*l+((m&0x3fff)<<14)+w[j]+c;
c = (l>>28)+(m>>14)+xh*h;
w[j++] = l&0xfffffff;
}
return c;
}
if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
BigInteger.prototype.am = am2;
dbits = 30;
}
else if(j_lm && (navigator.appName != "Netscape")) {
BigInteger.prototype.am = am1;
dbits = 26;
}
else { // Mozilla/Netscape seems to prefer am3
BigInteger.prototype.am = am3;
dbits = 28;
}
BigInteger.prototype.DB = dbits;
BigInteger.prototype.DM = ((1<<dbits)-1);
BigInteger.prototype.DV = (1<<dbits);
var BI_FP = 52;
BigInteger.prototype.FV = Math.pow(2,BI_FP);
BigInteger.prototype.F1 = BI_FP-dbits;
BigInteger.prototype.F2 = 2*dbits-BI_FP;
// Digit conversions
var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
var BI_RC = new Array();
var rr,vv;
rr = "0".charCodeAt(0);
for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
rr = "a".charCodeAt(0);
for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
rr = "A".charCodeAt(0);
for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
function int2char(n) { return BI_RM.charAt(n); }
function intAt(s,i) {
var c = BI_RC[s.charCodeAt(i)];
return (c==null)?-1:c;
}
// (protected) copy this to r
function bnpCopyTo(r) {
for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
r.t = this.t;
r.s = this.s;
}
// (protected) set from integer value x, -DV <= x < DV
function bnpFromInt(x) {
this.t = 1;
this.s = (x<0)?-1:0;
if(x > 0) this[0] = x;
else if(x < -1) this[0] = x+DV;
else this.t = 0;
}
// return bigint initialized to value
function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
// (protected) set from string and radix
function bnpFromString(s,b) {
var k;
if(b == 16) k = 4;
else if(b == 8) k = 3;
else if(b == 256) k = 8; // byte array
else if(b == 2) k = 1;
else if(b == 32) k = 5;
else if(b == 4) k = 2;
else { this.fromRadix(s,b); return; }
this.t = 0;
this.s = 0;
var i = s.length, mi = false, sh = 0;
while(--i >= 0) {
var x = (k==8)?s[i]&0xff:intAt(s,i);
if(x < 0) {
if(s.charAt(i) == "-") mi = true;
continue;
}
mi = false;
if(sh == 0)
this[this.t++] = x;
else if(sh+k > this.DB) {
this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
this[this.t++] = (x>>(this.DB-sh));
}
else
this[this.t-1] |= x<<sh;
sh += k;
if(sh >= this.DB) sh -= this.DB;
}
if(k == 8 && (s[0]&0x80) != 0) {
this.s = -1;
if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
}
this.clamp();
if(mi) BigInteger.ZERO.subTo(this,this);
}
// (protected) clamp off excess high words
function bnpClamp() {
var c = this.s&this.DM;
while(this.t > 0 && this[this.t-1] == c) --this.t;
}
// (public) return string representation in given radix
function bnToString(b) {
if(this.s < 0) return "-"+this.negate().toString(b);
var k;
if(b == 16) k = 4;
else if(b == 8) k = 3;
else if(b == 2) k = 1;
else if(b == 32) k = 5;
else if(b == 4) k = 2;
else return this.toRadix(b);
var km = (1<<k)-1, d, m = false, r = "", i = this.t;
var p = this.DB-(i*this.DB)%k;
if(i-- > 0) {
if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
while(i >= 0) {
if(p < k) {
d = (this[i]&((1<<p)-1))<<(k-p);
d |= this[--i]>>(p+=this.DB-k);
}
else {
d = (this[i]>>(p-=k))&km;
if(p <= 0) { p += this.DB; --i; }
}
if(d > 0) m = true;
if(m) r += int2char(d);
}
}
return m?r:"0";
}
// (public) -this
function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
// (public) |this|
function bnAbs() { return (this.s<0)?this.negate():this; }
// (public) return + if this > a, - if this < a, 0 if equal
function bnCompareTo(a) {
var r = this.s-a.s;
if(r != 0) return r;
var i = this.t;
r = i-a.t;
if(r != 0) return r;
while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
return 0;
}
// returns bit length of the integer x
function nbits(x) {
var r = 1, t;
if((t=x>>>16) != 0) { x = t; r += 16; }
if((t=x>>8) != 0) { x = t; r += 8; }
if((t=x>>4) != 0) { x = t; r += 4; }
if((t=x>>2) != 0) { x = t; r += 2; }
if((t=x>>1) != 0) { x = t; r += 1; }
return r;
}
// (public) return the number of bits in "this"
function bnBitLength() {
if(this.t <= 0) return 0;
return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
}
// (protected) r = this << n*DB
function bnpDLShiftTo(n,r) {
var i;
for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
for(i = n-1; i >= 0; --i) r[i] = 0;
r.t = this.t+n;
r.s = this.s;
}
// (protected) r = this >> n*DB
function bnpDRShiftTo(n,r) {
for(var i = n; i < this.t; ++i) r[i-n] = this[i];
r.t = Math.max(this.t-n,0);
r.s = this.s;
}
// (protected) r = this << n
function bnpLShiftTo(n,r) {
var bs = n%this.DB;
var cbs = this.DB-bs;
var bm = (1<<cbs)-1;
var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
for(i = this.t-1; i >= 0; --i) {
r[i+ds+1] = (this[i]>>cbs)|c;
c = (this[i]&bm)<<bs;
}
for(i = ds-1; i >= 0; --i) r[i] = 0;
r[ds] = c;
r.t = this.t+ds+1;
r.s = this.s;
r.clamp();
}
// (protected) r = this >> n
function bnpRShiftTo(n,r) {
r.s = this.s;
var ds = Math.floor(n/this.DB);
if(ds >= this.t) { r.t = 0; return; }
var bs = n%this.DB;
var cbs = this.DB-bs;
var bm = (1<<bs)-1;
r[0] = this[ds]>>bs;
for(var i = ds+1; i < this.t; ++i) {
r[i-ds-1] |= (this[i]&bm)<<cbs;
r[i-ds] = this[i]>>bs;
}
if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
r.t = this.t-ds;
r.clamp();
}
// (protected) r = this - a
function bnpSubTo(a,r) {
var i = 0, c = 0, m = Math.min(a.t,this.t);
while(i < m) {
c += this[i]-a[i];
r[i++] = c&this.DM;
c >>= this.DB;
}
if(a.t < this.t) {
c -= a.s;
while(i < this.t) {
c += this[i];
r[i++] = c&this.DM;
c >>= this.DB;
}
c += this.s;
}