-
Notifications
You must be signed in to change notification settings - Fork 38
/
__gen.cpp
executable file
·1042 lines (988 loc) · 28.9 KB
/
__gen.cpp
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
/** Micro Mezzo Macro Flation -- Overheated Economy ., Last Update: Feb. 12th
* 2013 **/ //{
/** Header .. **/ //{
#define LOCAL
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define FOR(i, a, b) for (int i = int(a); i < int(b); ++i)
#define DWN(i, b, a) for (int i = int(b - 1); i >= int(a); --i)
#define REP_1(i, n) for (int i = 1; i <= int(n); ++i)
#define FOR_1(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define DWN_1(i, b, a) for (int i = int(b); i >= int(a); --i)
#define REP_C(i, n) for (int n____ = int(n), i = 0; i < n____; ++i)
#define FOR_C(i, a, b) for (int b____ = int(b), i = a; i < b____; ++i)
#define DWN_C(i, b, a) for (int a____ = int(a), i = b - 1; i >= a____; --i)
#define REP_N(i, n) for (i = 0; i < int(n); ++i)
#define FOR_N(i, a, b) for (i = int(a); i < int(b); ++i)
#define DWN_N(i, b, a) for (i = int(b - 1); i >= int(a); --i)
#define REP_1_C(i, n) for (int n____ = int(n), i = 1; i <= n____; ++i)
#define FOR_1_C(i, a, b) for (int b____ = int(b), i = a; i <= b____; ++i)
#define DWN_1_C(i, b, a) for (int a____ = int(a), i = b; i >= a____; --i)
#define REP_1_N(i, n) for (i = 1; i <= int(n); ++i)
#define FOR_1_N(i, a, b) for (i = int(a); i <= int(b); ++i)
#define DWN_1_N(i, b, a) for (i = int(b); i >= int(a); --i)
#define REP_C_N(i, n) for (int n____ = (i = 0, int(n)); i < n____; ++i)
#define FOR_C_N(i, a, b) for (int b____=(i=0,int(b);i<b____;++i)
#define DWN_C_N(i, b, a) for (int a____ = (i = b - 1, int(a)); i >= a____; --i)
#define REP_1_C_N(i, n) for (int n____ = (i = 1, int(n)); i <= n____; ++i)
#define FOR_1_C_N(i, a, b) for (int b____=(i=1,int(b);i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (int a____ = (i = b, int(a)); i >= a____; --i)
#define ECH(it, A) for (__typeof(A.begin()) it = A.begin(); it != A.end(); ++it)
#define REP_S(i, str) for (char *i = str; *i; ++i)
#define REP_L(i, hd, nxt) for (int i = hd; i; i = nxt[i])
#define REP_G(i, u) REP_L(i, hd[u], suc)
#define DO(n) for (int ____n##__line__ = n; ____n##__line__--;)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)
#define REP_4(i, j, k, ii, n, m, l, nn) \
REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)
#define REP_4_1(i, j, k, ii, n, m, l, nn) \
REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, x) (lower_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define fi first
#define se second
#define Rush for (int ____T = RD(); ____T--;)
#define Display(A, n, m) \
{ \
REP(i, n) { \
REP(j, m) cout << A[i][j] << " "; \
cout << endl; \
} \
}
#define Display_1(A, n, m) \
{ \
REP_1(i, n) { \
REP_1(j, m) cout << A[i][j] << " "; \
cout << endl; \
} \
}
#pragma comment(linker, "/STACK:36777216")
//#pragma GCC optimize ("O2")
#define Ruby system("ruby main.rb")
#define Haskell system("runghc main.hs")
#define Python system("python main.py")
#define Pascal system("fpc main.pas")
typedef long long LL;
// typedef long double DB;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
typedef set<int> SI;
typedef set<string> SS;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
template <class T>
inline T &RD(T &);
template <class T>
inline void OT(const T &);
inline LL RD() {
LL x;
return RD(x);
}
inline DB &RF(DB &);
inline DB RF() {
DB x;
return RF(x);
}
inline char *RS(char *s);
inline char &RC(char &c);
inline char RC();
inline char &RC(char &c) {
scanf(" %c", &c);
return c;
}
inline char RC() {
char c;
return RC(c);
}
// inline char& RC(char &c){c = getchar(); return c;}
// inline char RC(){return getchar();}
template <class T>
inline T &RDD(T &x) {
char c;
for (c = getchar(); c < '-'; c = getchar())
;
if (c == '-') {
x = '0' - getchar();
for (c = getchar(); '0' <= c && c <= '9'; c = getchar())
x = x * 10 + '0' - c;
} else {
x = c - '0';
for (c = getchar(); '0' <= c && c <= '9'; c = getchar())
x = x * 10 + c - '0';
}
return x;
}
inline LL RDD() {
LL x;
return RDD(x);
}
template <class T0, class T1>
inline T0 &RD(T0 &x0, T1 &x1) {
RD(x0), RD(x1);
return x0;
}
template <class T0, class T1, class T2>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
RD(x0), RD(x1), RD(x2);
return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
RD(x0), RD(x1), RD(x2), RD(x3);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
return x0;
}
template <class T0, class T1>
inline void OT(const T0 &x0, const T1 &x1) {
OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5, const T6 &x6) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
RC(a), RC(b);
return a;
}
inline char &RC(char &a, char &b, char &c) {
RC(a), RC(b), RC(c);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
RC(a), RC(b), RC(c), RC(d);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
RC(a), RC(b), RC(c), RC(d), RC(e);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
return a;
}
inline DB &RF(DB &a, DB &b) {
RF(a), RF(b);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c) {
RF(a), RF(b), RF(c);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d) {
RF(a), RF(b), RF(c), RF(d);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e) {
RF(a), RF(b), RF(c), RF(d), RF(e);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
return a;
}
inline DB &RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T0, class T1>
inline void RDD(const T0 &a, const T1 &b) {
RDD(a), RDD(b);
}
template <class T0, class T1, class T2>
inline void RDD(const T0 &a, const T1 &b, const T2 &c) {
RDD(a), RDD(b), RDD(c);
}
template <class T>
inline void RST(T &A) {
memset(A, 0, sizeof(A));
}
template <class T>
inline void FLC(T &A, int x) {
memset(A, x, sizeof(A));
}
template <class T>
inline void CLR(T &A) {
A.clear();
}
template <class T0, class T1>
inline void RST(T0 &A0, T1 &A1) {
RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T0, class T1>
inline void FLC(T0 &A0, T1 &A1, int x) {
FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x),
FLC(A6, x);
}
template <class T>
inline void CLR(priority_queue<T, vector<T>, less<T> > &Q) {
while (!Q.empty()) Q.pop();
}
template <class T>
inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) {
while (!Q.empty()) Q.pop();
}
template <class T0, class T1>
inline void CLR(T0 &A0, T1 &A1) {
CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T>
inline void CLR(T &A, int n) {
REP(i, n) CLR(A[i]);
}
template <class T>
inline T &SRT(T &A) {
sort(ALL(A));
return A;
}
template <class T, class C>
inline T &SRT(T &A, C B) {
sort(ALL(A), B);
return A;
}
template <class T>
inline T &UNQ(T &A) {
A.resize(unique(ALL(SRT(A))) - A.begin());
return A;
}
//}
/** Constant List .. **/ //{
const int dx4[] = {-1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy8[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const LL MOD = 1000000007;
// int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e15;
const DB PI = acos(-1.0); // M_PI;
//}
/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{
template <class T>
inline void checkMin(T &a, const T b) {
if (b < a) a = b;
}
template <class T>
inline void checkMax(T &a, const T b) {
if (a < b) a = b;
}
template <class T>
inline void checkMin(T &a, T &b, const T x) {
checkMin(a, x), checkMin(b, x);
}
template <class T>
inline void checkMax(T &a, T &b, const T x) {
checkMax(a, x), checkMax(b, x);
}
template <class T, class C>
inline void checkMin(T &a, const T b, C c) {
if (c(b, a)) a = b;
}
template <class T, class C>
inline void checkMax(T &a, const T b, C c) {
if (c(a, b)) a = b;
}
template <class T>
inline T min(T a, T b, T c) {
return min(min(a, b), c);
}
template <class T>
inline T max(T a, T b, T c) {
return max(max(a, b), c);
}
template <class T>
inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T>
inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
template <class T>
inline T sqr(T a) {
return a * a;
}
template <class T>
inline T cub(T a) {
return a * a * a;
}
inline int ceil(int x, int y) { return (x - 1) / y + 1; }
inline int sgn(DB x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(DB x, DB y) { return sgn(x - y); }
//}
// <<= '1. Bitwise Operation ., //{
namespace BO {
inline bool _1(int x, int i) { return bool(x & 1 << i); }
inline bool _1(LL x, int i) { return bool(x & 1LL << i); }
inline LL _1(int i) { return 1LL << i; }
inline LL _U(int i) { return _1(i) - 1; };
inline int reverse_bits(int x) {
x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000);
return x;
}
inline LL reverse_bits(LL x) {
x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
x = ((x >> 16) & 0x0000ffff0000ffffLL) | ((x << 16) & 0xffff0000ffff0000LL);
x = ((x >> 32) & 0x00000000ffffffffLL) | ((x << 32) & 0xffffffff00000000LL);
return x;
}
template <class T>
inline bool odd(T x) {
return x & 1;
}
template <class T>
inline bool even(T x) {
return !odd(x);
}
template <class T>
inline T low_bit(T x) {
return x & -x;
}
template <class T>
inline T high_bit(T x) {
T p = low_bit(x);
while (p != x) x -= p, p = low_bit(x);
return p;
}
template <class T>
inline T cover_bit(T x) {
T p = 1;
while (p < x) p <<= 1;
return p;
}
inline int low_idx(int x) { return __builtin_ffs(x); }
inline int low_idx(LL x) { return __builtin_ffsll(x); }
inline int high_idx(int x) { return low_idx(reverse_bits(x)); }
inline int high_idx(LL x) { return low_idx(reverse_bits(x)); }
inline int clz(int x) { return __builtin_clz(x); }
inline int clz(LL x) { return __builtin_clzll(x); }
inline int ctz(int x) { return __builtin_ctz(x); }
inline int ctz(LL x) { return __builtin_ctzll(x); }
inline int parity(int x) { return __builtin_parity(x); }
inline int parity(LL x) { return __builtin_parityll(x); }
inline int lg2(int a) { return 31 - clz(a); }
inline int lg2(LL a) { return 63 - clz(a); }
inline int count_bits(int x) { return __builtin_popcount(x); }
inline int count_bits(LL x) { return __builtin_popcountll(x); }
} // namespace BO
using namespace BO; //}
// <<= '2. Number Theory .,//{
namespace NT {
inline LL __lcm(LL a, LL b) { return a * b / __gcd(a, b); }
inline void INC(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
inline int sum(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
inline void DEC(int &a, int b) {
a -= b;
if (a < 0) a += MOD;
}
inline int dff(int a, int b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
inline void MUL(int &a, int b) { a = (LL)a * b % MOD; }
inline int pdt(int a, int b) { return (LL)a * b % MOD; }
inline int sum(int a, int b, int c) { return sum(sum(a, b), c); }
inline int sum(int a, int b, int c, int d) { return sum(sum(a, b), sum(c, d)); }
inline int pdt(int a, int b, int c) { return pdt(pdt(a, b), c); }
inline int pdt(int a, int b, int c, int d) { return pdt(pdt(pdt(a, b), c), d); }
inline int pow(int a, int b) {
int c(1);
while (b) {
if (b & 1) MUL(c, a);
MUL(a, a), b >>= 1;
}
return c;
}
inline int pow(int a, LL b) {
int c(1);
while (b) {
if (b & 1) MUL(c, a);
MUL(a, a), b >>= 1;
}
return c;
}
template <class T>
inline T pow(T a, LL b) {
T c(1);
while (b) {
if (b & 1) c *= a;
a *= a, b >>= 1;
}
return c;
}
inline int _I(int b) {
int a = MOD, x1 = 0, x2 = 1, q;
while (true) {
q = a / b, a %= b;
if (!a) return (x2 + MOD) % MOD;
DEC(x1, pdt(q, x2));
q = b / a, b %= a;
if (!b) return (x1 + MOD) % MOD;
DEC(x2, pdt(q, x1));
}
}
inline void DIV(int &a, int b) { MUL(a, _I(b)); }
inline int qtt(int a, int b) { return pdt(a, _I(b)); }
inline int phi(int n) {
int res = n;
for (int i = 2; sqr(i) <= n; ++i)
if (!(n % i)) {
DEC(res, qtt(res, i));
do {
n /= i;
} while (!(n % i));
}
if (n != 1) DEC(res, qtt(res, n));
return res;
}
}; // namespace NT
//}
/** Miscellaneous .. **/ //{
// <<= 'Random Event .. . //{
namespace RNG {
// srand((unsigned)time(NULL));
inline unsigned int rand16() { return ((rand()) << 15) ^ rand(); }
inline unsigned int rand32() { return (rand16() << 16) | rand16(); }
inline ULL rand64() { return ((LL)rand32() << 32) | rand32(); }
inline ULL random(LL l, LL r) { return l == r ? l : rand64() % (r - l) + l; }
int dice() { return rand() % 6; }
bool coin() { return bool(rand() % 2); }
} // namespace RNG
using namespace RNG;
//}
// <<= 'Clock .. . //{
namespace CLOCK {
DB s0, s1, rd, k, T;
inline DB getTime() {
#ifdef LOCAL
return 1.0 * clock() / CLOCKS_PER_SEC;
#else
timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + tv.tv_usec * 1e-6;
#endif
}
inline void st0(DB _T = 0.98) { T = _T, s0 = getTime(); }
inline void st1(DB _k = 1.618) { k = _k, s1 = getTime(); }
inline void ed1() { rd = getTime() - s1; }
inline DB elapsed() { return getTime() - s0; }
inline bool safe() { return elapsed() + rd * k < T; }
} // namespace CLOCK
//}
// <<= 'Temp .. . //{
namespace TMP {
template <class T>
PTT operator+(const PTT &p1, const PTT &p2) {
return PTT(p1.fi + p2.fi, p1.se + p2.se);
}
template <class T>
PTT operator-(const PTT &p1, const PTT &p2) {
return PTT(p1.fi - p2.fi, p1.se - p2.se);
}
template <class T>
PTT operator*(const PTT &lhs, T k) {
return PTT(lhs.fi * k, lhs.se * k);
}
} // namespace TMP
using namespace TMP;
//}
//}
/** Algorithm .. */ //{
// <<= '-. Math .,//{
namespace Math {
typedef long long typec;
/// Lib functions
typec GCD(typec a, typec b) { return b ? GCD(b, a % b) : a; }
typec extendGCD(typec a, typec b, typec &x, typec &y) {
if (!b) return x = 1, y = 0, a;
typec res = extendGCD(b, a % b, x, y), tmp = x;
x = y, y = tmp - (a / b) * y;
return res;
}
/// for x^k
typec power(typec x, typec k) {
typec res = 1;
while (k) {
if (k & 1) res *= x;
x *= x, k >>= 1;
}
return res;
}
/// for x^k mod m
typec powerMod(typec x, typec k, typec m) {
typec res = 1;
while (x %= m, k) {
if (k & 1) res *= x, res %= m;
x *= x, k >>= 1;
}
return res;
}
/***************************************
Inverse in mod p^t system
***************************************/
typec inverse(typec a, typec p, typec t = 1) {
typec pt = power(p, t);
typec x, y;
y = extendGCD(a, pt, x, y);
return x < 0 ? x += pt : x;
}
/***************************************
Linear congruence theorem
x = a (mod p)
x = b (mod q)
for gcd(p, q) = 1, 0 <= x < pq
***************************************/
typec linearCongruence(typec a, typec b, typec p, typec q) {
typec x, y;
y = extendGCD(p, q, x, y);
while (b < a) b += q / y;
x *= b - a, x = p * x + a, x %= p * q;
if (x < 0) x += p * q;
return x;
}
/***************************************
prime table
O(n)
***************************************/
const int PRIMERANGE = 1000000;
int prime[PRIMERANGE + 1];
int getPrime() {
memset(prime, 0, sizeof(int) * (PRIMERANGE + 1));
for (int i = 2; i <= PRIMERANGE; i++) {
if (!prime[i]) prime[++prime[0]] = i;
for (int j = 1; j <= prime[0] && prime[j] <= PRIMERANGE / i; j++) {
prime[prime[j] * i] = 1;
if (i % prime[j] == 0) break;
}
}
return prime[0];
}
/***************************************
get factor of n
O(sqrt(n))
factor[][0] is prime factor
factor[][1] is factor generated by this prime
factor[][2] is factor counter
need: Prime Table
***************************************/
/// you should init the prime table before
int factor[100][3], facCnt;
int getFactors(int x) {
facCnt = 0;
int tmp = x;
for (int i = 1; prime[i] <= tmp / prime[i]; i++) {
factor[facCnt][1] = 1, factor[facCnt][2] = 0;
if (tmp % prime[i] == 0) factor[facCnt][0] = prime[i];
while (tmp % prime[i] == 0)
factor[facCnt][2]++, factor[facCnt][1] *= prime[i], tmp /= prime[i];
if (factor[facCnt][1] > 1) facCnt++;
}
if (tmp != 1)
factor[facCnt][0] = tmp, factor[facCnt][1] = tmp, factor[facCnt++][2] = 1;
return facCnt;
}
typec combinationModP(typec n, typec k, typec p) {
if (k > n) return 0;
if (n - k < k) k = n - k;
typec a = 1, b = 1, x, y;
int pcnt = 0;
for (int i = 1; i <= k; i++) {
x = n - i + 1, y = i;
while (x % p == 0) x /= p, pcnt++;
while (y % p == 0) y /= p, pcnt--;
x %= p, y %= p, a *= x, b *= y;
b %= p, a %= p;
}
if (pcnt) return 0;
extendGCD(b, p, x, y);
if (x < 0) x += p;
a *= x, a %= p;
return a;
}
}; // namespace Math
//}
// <<= '-. Geo ,.//{
namespace Geo {
#define typec double
const typec eps = 1e-8;
int dblcmp(double d) {
if (fabs(d) < eps) return 0;
return d > eps ? 1 : -1;
}
int sgn(double a) { return a < -eps ? -1 : a > eps; }
inline double sqr(double x) { return x * x; }
struct Point2D {
typec x, y;
Point2D() {}
Point2D(typec _x, typec _y) : x(_x), y(_y){};
void input() { scanf("%lf%lf", &x, &y); }
void output() { printf("%.2f %.2f\n", x, y); }
bool operator==(Point2D a) const {
return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0;
}
bool operator<(Point2D a) const {
return dblcmp(a.x - x) == 0 ? dblcmp(y - a.y) < 0 : x < a.x;
}
typec len() { return hypot(x, y); }
typec len2() { return x * x + y * y; }
Point2D operator+(const Point2D &A) const {
return Point2D(x + A.x, y + A.y);
}
Point2D operator-(const Point2D &A) const {
return Point2D(x - A.x, y - A.y);
}
Point2D operator*(const typec _x) const { return Point2D(x * _x, y * _x); }
typec operator*(const Point2D &A) const { return x * A.x + y * A.y; }
typec operator^(const Point2D &A) const { return x * A.y - y * A.x; }
Point2D operator/(const typec _p) const { return Point2D(x / _p, y / _p); }
typec distance(Point2D p) { return hypot(x - p.x, y - p.y); }
Point2D add(Point2D p) { return Point2D(x + p.x, y + p.y); }
Point2D sub(Point2D p) { return Point2D(x - p.x, y - p.y); }
Point2D mul(typec b) { return Point2D(x * b, y * b); }
Point2D div(typec b) { return Point2D(x / b, y / b); }
typec dot(Point2D p) { return x * p.x + y * p.y; }
typec det(Point2D p) { return x * p.y - y * p.x; }
typec rad(Point2D a, Point2D b) {
Point2D p = *this;
return fabs(atan2(fabs(a.sub(p).det(b.sub(p))), a.sub(p).dot(b.sub(p))));
}
Point2D trunc(typec r) {
typec l = len();
if (!dblcmp(l)) return *this;
r /= l;
return Point2D(x * r, y * r);
}
Point2D rotleft() { return Point2D(-y, x); }
Point2D rotright() { return Point2D(y, -x); }
Point2D rotate(Point2D p, typec angle) //ÈƵãpÄæʱÕëÐýתangle½Ç¶È
{
Point2D v = this->sub(p);
typec c = cos(angle), s = sin(angle);
return Point2D(p.x + v.x * c - v.y * s, p.y + v.x * s + v.y * c);
}
};
typec cross(Point2D a, Point2D b, Point2D c) {
return (b.sub(a)).det(c.sub(a));
}
} // namespace Geo
using namespace Geo;
//}
//}
/** I/O Accelerator Interface .. **/ //{
template <class T>
inline T &RD(T &x) {
// cin >> x;
// scanf("%d", &x);
char c;
for (c = getchar(); c < '0'; c = getchar())
;
x = c - '0';
for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
// char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c =
// getchar()) x = x * 10 + c - '0';
return x;
}
inline DB &RF(DB &x) {
// cin >> x;
scanf("%lf", &x);
/*char t; while ((t=getchar())==' '||t=='\n'); x = t - '0';
while ((t=getchar())!=' '&&t!='\n'&&t!='.')x*=10,x+=t-'0';
if (t=='.'){DB l=1; while ((t=getchar())!=' '&&t!='\n')l*=0.1,x +=
(t-'0')*l;}*/
return x;
}
inline char *RS(char *s) {
// gets(s);
scanf("%s", s);
return s;
}
int Case;
template <class T>
inline void OT(const T &x) {
// printf("Case %d: %d\n", ++Case, x);
// printf("%.2lf\n", x);
// printf("%d\n", x);
cout << x << endl;
}
//}
/* ..................................................................................................................................
*/
LL cnt[20][2][7][7], dp[20][2][7][7], dpsum[20][2][7][7];
LL ten[20];
LL num[20];
void init() {
ten[0] = 1;
REP_1(i, 19) ten[i] = (ten[i - 1] * 10) % MOD;
}
LL dfscnt(int i, bool seven, int numbersum, int sum, bool e) {
if (i == -1) return (seven || numbersum % 7 == 0 || sum % 7 == 0) ? 1 : 0;
if (!e && ~cnt[i][seven][numbersum][sum])
return cnt[i][seven][numbersum][sum];
LL res = 0;
int u = e ? num[i] : 9;
for (int d = 0; d <= u; ++d)
res = (res + dfscnt(i - 1, (seven || d == 7), (numbersum + d) % 7,
(sum * 10 + d) % 7, e && d == u)) %
MOD;
return e ? res : cnt[i][seven][numbersum][sum] = res;
}
LL mul(LL x, LL y) {
x %= MOD;
y %= MOD;
return (x * y) % MOD;
}
LL dfssum(int i, int seven, int numbersum, int sum, bool e) {
if (i == -1) return 0;
if (!e && ~dpsum[i][seven][numbersum][sum])
return dpsum[i][seven][numbersum][sum];
LL res = 0;
int u = e ? num[i] : 9;
for (int d = 0; d <= u; ++d) {
LL has = dfscnt(i - 1, (seven || d == 7), (numbersum + d) % 7,
(sum * 10 + d) % 7, e && d == u);
LL tmp = mul(d, ten[i]);
tmp = mul(has, tmp);
res = (res + dfssum(i - 1, (seven || d == 7), (numbersum + d) % 7,
(sum * 10 + d) % 7, e && d == u)) %
MOD;
res = (res + tmp) % MOD;
}
return e ? res : dpsum[i][seven][numbersum][sum] = res;
}
LL dfs(int i, int seven, int numbersum, int sum, bool e) {
if (i == -1) return 0;
if (!e && ~dp[i][seven][numbersum][sum]) return dp[i][seven][numbersum][sum];
LL res = 0;
int u = e ? num[i] : 9;
for (int d = 0; d <= u; ++d) {
LL has = dfscnt(i - 1, (seven || d == 7), (numbersum + d) % 7,
(sum * 10 + d) % 7, e && d == u);
LL sum1 = dfssum(i - 1, (seven || d == 7), (numbersum + d) % 7,
(sum * 10 + d) % 7, e && d == u);
LL tmp = mul(d, ten[i]);
LL nownow = mul(tmp, tmp);
LL hasnow = mul(nownow, has);
nownow = mul(2, mul(tmp, sum1));
res = (res + dfs(i - 1, (seven || d == 7), (numbersum + d) % 7,
(sum * 10 + d) % 7, e && d == u)) %
MOD;
res = (res + hasnow) % MOD;
res = (res + nownow) % MOD;
}
return e ? res : dp[i][seven][numbersum][sum] = res;
}
LL l, r;
LL gao(LL x) {
LL a = x, b = x + 1, c = 2 * x + 1;
LL p = 2;
if (a % p == 0)
a /= p;
else if (b % p == 0)
b /= p;
else if (c % p == 0)
c /= p;
p = 3;
if (a % p == 0)
a /= p;