-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
882 lines (768 loc) · 30.4 KB
/
main.cpp
File metadata and controls
882 lines (768 loc) · 30.4 KB
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
#include <stxxl/queue>
#include <array>
#include <chrono>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <thread>
template<typename IntegralType = unsigned long long, unsigned FunctionsCount = 3, typename ValueType = IntegralType, typename QueueType = stxxl::queue<ValueType>, IntegralType InitValue = 1>
class Queue0 {
std::array<QueueType, FunctionsCount> m_queues;
std::array<IntegralType, FunctionsCount> m_a, m_b;
ValueType m_front;
public:
typedef ValueType ValueType;
static constexpr IntegralType initValue = InitValue;
template<typename... Pairs, std::enable_if_t<sizeof...(Pairs) == FunctionsCount, bool> = true>
explicit
Queue0(Pairs... p) : m_a{static_cast<IntegralType>(p.first)...}, m_b{static_cast<IntegralType>(p.second)...},
m_front{InitValue} {}
template<typename OtherIntegral>
explicit Queue0(const std::array<std::pair<OtherIntegral, OtherIntegral>, FunctionsCount>& functions) : m_front{InitValue} {
for (std::size_t i = 0; i < FunctionsCount; ++i) {
m_a[i] = static_cast<IntegralType>(functions[i].first);
m_b[i] = static_cast<IntegralType>(functions[i].second);
}
}
ValueType Process() {
auto value = m_front;
std::size_t toPop = 0;
for (std::size_t i = 0; i != FunctionsCount; ++i) {
auto y = value * m_a[i] + m_b[i];
m_queues[i].push(y);
if (m_queues[i].front() < m_queues[toPop].front()) {
toPop = i;
}
}
m_front = m_queues[toPop].front();
m_queues[toPop].pop();
for (std::size_t i = toPop + 1; i != FunctionsCount; ++i) {
if (m_queues[i].front() == m_queues[toPop].front()) {
m_front, m_queues[i].front();
m_queues[i].pop();
}
}
return value;
}
};
template<typename MyQueue = Queue0<>, std::size_t DeltaMax = 100, std::size_t ValueLimit = 1'000'000'000>
void main4(unsigned long long a2 = 0, unsigned long long a3 = 2, unsigned long long a6 = 3) {
std::string id = "main4_";
id += std::to_string(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch()).count()) + "_";
std::cout << "ID: " << id << std::endl;
id += std::to_string(a2) + "_" + std::to_string(a3) + "_" + std::to_string(a6);
std::ofstream log(id);
constexpr std::size_t deltaMax = DeltaMax;
constexpr std::size_t valueLimit = ValueLimit;
auto startTimePoint = std::chrono::system_clock::now();
MyQueue queue(std::make_pair(2, a2), std::make_pair(3, a3), std::make_pair(6, a6));
std::array<std::vector<unsigned long long>, deltaMax> popped;
for (std::size_t deltaM1 = 0; deltaM1 < deltaMax; ++deltaM1) {
popped[deltaM1].resize(deltaM1 + 1);
}
for (auto v = queue.Process(); v < valueLimit; v = queue.Process()) {
//std::cout << v << std::endl;
for (std::size_t deltaM1 = 0; deltaM1 < deltaMax; ++deltaM1) {
++popped[deltaM1][v % (deltaM1 + 1)];
}
}
log << "% " << std::chrono::duration_cast<std::chrono::milliseconds>((std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
std::set<std::tuple<double, std::size_t, std::size_t>> best;
for (std::size_t deltaM1 = 0; deltaM1 < deltaMax; ++deltaM1) {
for (std::size_t r = 0; r != deltaM1 + 1; ++r) {
best.emplace(popped[deltaM1][r] / static_cast<double>((valueLimit + deltaM1 - r) / (deltaM1 + 1)), deltaM1 + 1, r);
if (best.size() > 10) {
best.erase(best.begin());
}
}
}
for (auto [density, delta, r] : best) {
log << density << "\t& " << delta << "\t& " << r << " \\\\" << std::endl;
}
}
typedef unsigned long long Integral;
#define COMPARABLE_AS_OP(LA, RA, OP) \
template<typename Other> \
auto operator OP(Other ra) const { \
auto other = std::remove_reference_t<decltype(*this)>(ra); \
return LA OP RA; \
}
#define COMPARABLE_AS(LA, RA) \
COMPARABLE_AS_OP(LA, RA, ==) \
COMPARABLE_AS_OP(LA, RA, !=) \
COMPARABLE_AS_OP(LA, RA, <=) \
COMPARABLE_AS_OP(LA, RA, >=) \
COMPARABLE_AS_OP(LA, RA, <) \
COMPARABLE_AS_OP(LA, RA, >)
template<typename Integral>
Integral GCD(Integral a, Integral b) {
while (a != 0) {
Integral t = a;
a = b % a;
b = t;
}
return b;
}
template<typename Integral>
Integral LCM(Integral a, Integral b) {
return a / GCD(a, b) * b;
}
class SafeIntegral {
static constexpr Integral MAX_VALUE = ~Integral(0);
Integral value;
public:
explicit SafeIntegral(Integral value = Integral()) : value(value) {}
SafeIntegral& operator+=(SafeIntegral other) {
if (MAX_VALUE - value >= other.value) {
value += other.value;
return *this;
} else {
throw std::overflow_error("unsigned long long overflowed");
}
}
SafeIntegral operator+(SafeIntegral other) const {
SafeIntegral res = *this;
return res += other;
}
SafeIntegral& operator-=(SafeIntegral other) {
value -= other.value;
return *this;
}
SafeIntegral operator-(SafeIntegral other) const {
SafeIntegral res = *this;
return res -= other;
}
SafeIntegral& operator*=(SafeIntegral other) {
if (value == 0 || MAX_VALUE / value >= other.value) {
value *= other.value;
return *this;
} else {
throw std::overflow_error("unsigned long long overflowed");
}
}
SafeIntegral operator*(SafeIntegral other) const {
SafeIntegral res = *this;
return res *= other;
}
SafeIntegral& operator/=(SafeIntegral other) {
value /= other.value;
return *this;
}
SafeIntegral operator/(SafeIntegral other) const {
SafeIntegral res = *this;
return res /= other;
}
SafeIntegral& operator%=(SafeIntegral other) {
value %= other.value;
return *this;
}
SafeIntegral operator%(SafeIntegral other) const {
SafeIntegral res = *this;
return res %= other;
}
COMPARABLE_AS(value, other.value)
[[nodiscard]] Integral toIntegral() const {
return value;
}
explicit operator Integral() const {
return value;
}
};
class Rational {
SafeIntegral numerator = SafeIntegral(0);
SafeIntegral denominator = SafeIntegral(1);
static SafeIntegral Reduce(SafeIntegral& a, SafeIntegral& b) {
SafeIntegral d = GCD(a, b);
a /= d;
b /= d;
return d;
}
public:
Rational() = default;
explicit Rational(Integral value) : numerator(value) {}
Rational(Integral numerator, Integral denominator) : numerator(numerator), denominator(denominator) {}
explicit Rational(SafeIntegral numerator, SafeIntegral denominator = SafeIntegral(1)) : numerator(numerator), denominator(denominator) {}
Rational& operator++() {
numerator += denominator;
return *this;
}
Rational operator+=(Rational other) {
auto d = Reduce(denominator, other.denominator);
numerator *= other.denominator;
other.numerator *= denominator;
numerator += other.numerator;
Reduce(numerator, d);
denominator *= d;
denominator *= other.denominator;
return *this;
}
Rational operator+(Rational other) const {
Rational res = *this;
return res += other;
}
Rational operator-=(Rational other) {
auto d = Reduce(denominator, other.denominator);
numerator *= other.denominator;
other.numerator *= denominator;
numerator -= other.numerator;
Reduce(numerator, d);
denominator *= d;
denominator *= other.denominator;
return *this;
}
Rational operator-(Rational other) const {
Rational res = *this;
return res -= other;
}
Rational operator/=(Rational other) {
Reduce(numerator, other.numerator);
Reduce(denominator, other.denominator);
numerator *= other.denominator;
denominator *= other.numerator;
return *this;
}
Rational operator/(Rational other) const {
Rational res = *this;
return res /= other;
}
Rational operator*=(Rational other) {
Reduce(numerator, other.denominator);
Reduce(denominator, other.numerator);
numerator *= other.numerator;
denominator *= other.denominator;
return *this;
}
Rational operator*(Rational other) const {
Rational res = *this;
return res *= other;
}
COMPARABLE_AS(numerator * other.denominator, denominator * other.numerator)
static Rational Ceil(Rational r) {
return Rational(r.numerator / r.denominator + SafeIntegral(r.numerator % r.denominator != 0 ? 1 : 0));
}
static Rational Floor(Rational r) {
return Rational(r.numerator / r.denominator);
}
static bool IsInteger(Rational rational) {
return rational.denominator == 1;
}
explicit operator SafeIntegral() const {
return numerator / denominator;
}
[[nodiscard]] SafeIntegral GetNumerator() const {
return numerator;
}
[[nodiscard]] SafeIntegral GetDenominator() const {
return denominator;
}
[[nodiscard]] static SafeIntegral GetNumerator(Rational r) {
return r.numerator;
}
[[nodiscard]] static SafeIntegral GetDenominator(Rational r) {
return r.denominator;
}
};
Rational operator/(Integral x, Rational other) {
auto res = Rational(x);
return res /= other;
}
template<Integral N>
struct Trie {
Rational first;
std::vector<Trie<N - 1>> rest;
Trie() = default;
Trie(Rational first, std::vector<Trie<N - 1>>&& rest) : first(first), rest(std::move(rest)) {}
static std::vector<Trie> Search(Rational sum = Rational(1), Rational start = Rational(2)) {
auto mean = sum / Rational(N);
std::vector<Trie<N>> res;
for (Rational a = std::max(Rational::Floor(1 / sum) + Rational(1), start); 1 / a > mean; ++a) {
auto rest = Trie<N - 1>::Search(sum - 1 / a, a + Rational(1));
res.emplace_back(a, std::move(rest));
}
return std::move(res);
}
};
template<>
struct Trie<1> {
Rational first;
Trie() = default;
explicit Trie(Rational first) : first(first) {}
static std::vector<Trie> Search(Rational sum, Rational start) {
std::vector<Trie<1>> res;
if (Rational a = 1 / sum; Rational::IsInteger(a) && a >= start) {
res.emplace_back(a);
}
return std::move(res);
}
};
template<Integral N>
void Iterate(const std::vector<Trie<N>>& tries, std::vector<SafeIntegral>& buffer, std::function<void(const std::vector<SafeIntegral>&)>& callback) {
for (auto& trie : tries) {
buffer.emplace_back(trie.first);
Iterate(trie.rest, buffer, callback);
buffer.pop_back();
}
}
std::ostream& operator<<(std::ostream& out, const SafeIntegral& v) {
return out << v.toIntegral();
}
template<>
void Iterate<1>(const std::vector<Trie<1>>& tries, std::vector<SafeIntegral>& buffer, std::function<void(const std::vector<SafeIntegral>&)>& callback) {
for (auto& trie : tries) {
buffer.emplace_back(trie.first);
callback(buffer);
buffer.pop_back();
}
}
template<Integral N>
void Iterate(const std::vector<Trie<N>>& tries, std::function<void(const std::vector<SafeIntegral>&)> callback) {
std::vector<SafeIntegral> buffer;
buffer.reserve(N);
Iterate<N>(tries, buffer, callback);
}
template<Integral FunctionsCount = 4>
void main2() {
auto startTimePoint = std::chrono::system_clock::now();
auto trie = Trie<FunctionsCount>::Search();
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
Integral counter = 0;
Integral maxDenominator = 0;
Iterate(trie, [&counter, &maxDenominator](const std::vector<SafeIntegral>& v) {
++counter;
if (v.back() > maxDenominator) {
maxDenominator = v.back().toIntegral();
}
});
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
std::cout << counter << ' ' << maxDenominator << '\n';
}
template<Integral FunctionsCount = 4>
void SearchAndIterate(std::function<void(const std::vector<SafeIntegral>&)> callback) {
auto startTimePoint = std::chrono::system_clock::now();
auto trie = Trie<FunctionsCount>::Search();
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
Iterate(trie, callback);
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
}
template<typename MyQueue, std::size_t ValueLimit = 10'000'000'000>
void maxDensityDeltaLogging(unsigned long long a2 = 0, unsigned long long a3 = 2, unsigned long long a6 = 3) {
std::string id = "maxDensityDeltaLogging" "_";
// id += std::to_string(std::chrono::duration_cast<std::chrono::seconds>(
// std::chrono::system_clock::now().time_since_epoch()).count()) + "_";
id += std::to_string(a2) + "_" + std::to_string(a3) + "_" + std::to_string(a6);
std::ofstream log(id);
auto startTimePoint = std::chrono::system_clock::now();
MyQueue queue(std::make_pair(2, a2), std::make_pair(3, a3), std::make_pair(6, a6));
double densityDelta;
unsigned long long maxDelta = 0;
unsigned long long v, startDelta;
unsigned long long prev = 0;
unsigned long long popped = 0;
constexpr unsigned long long LOG_STEP = 2;
unsigned long long nextLogged = LOG_STEP;
do {
v = queue.Process();
auto delta = v - prev;
double dd = static_cast<double>(popped + 1) / (v + 1) - static_cast<double>(popped) / (prev + 1);
if (maxDelta == 0 || densityDelta < dd) {
startDelta = prev;
maxDelta = delta;
densityDelta = dd;
}
while (v >= nextLogged) {
if (maxDelta > 0) {
log << maxDelta << '\t' << startDelta << '\t' << densityDelta << std::endl;
}
nextLogged *= LOG_STEP;
maxDelta = 0;
}
prev = v;
++popped;
} while (v < ValueLimit);
log.close();
std::cout << "% " << id << ": " << std::chrono::duration_cast<std::chrono::milliseconds>((std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
}
template<typename ValueType>
struct WithMultiplicity {
ValueType value;
unsigned long long multiplicity;
explicit WithMultiplicity(ValueType v = 0) : value(v), multiplicity(1) {};
WithMultiplicity& operator,(WithMultiplicity other) {
multiplicity += other.multiplicity;
return *this;
};
WithMultiplicity& operator*=(ValueType other) {
value *= other;
return *this;
}
WithMultiplicity operator*(ValueType other) {
WithMultiplicity res = *this;
res *= other;
return res;
}
WithMultiplicity& operator+=(ValueType other) {
value += other;
return *this;
}
WithMultiplicity operator+(ValueType other) {
WithMultiplicity res = *this;
res += other;
return res;
}
COMPARABLE_AS(value, other.value)
ValueType operator-(ValueType other) {
return value - other;
}
explicit operator ValueType() {
return value;
}
};
template<typename... F>
class Processor {
std::tuple<F...> m_callbacks;
public:
explicit Processor(F&&... callbacks) : m_callbacks(std::move(callbacks)...) {}
template<typename QueueType = Queue0<>, std::size_t ValueLimit = 10'000'000'000>
void Process(QueueType& queue) {
typename QueueType::ValueType v;
do {
v = queue.Process();
bool needToStop = false;
std::initializer_list<int> _ = {(needToStop = std::get<F>(m_callbacks)(v) || needToStop, 42)...};
if (needToStop) {
break;
}
} while (v < ValueLimit);
}
};
class DensityLogger {
static constexpr Integral LOG_STEP = 2;
std::ofstream m_out;
Integral m_nextLogged = LOG_STEP;
Integral m_popped = 0;
public:
explicit DensityLogger(const std::string& filename) : m_out(filename, std::ios::out) {}
DensityLogger(const DensityLogger&) = default;
DensityLogger(DensityLogger&&) = default;
~DensityLogger() {
m_out.close();
}
template<typename ValueType>
bool operator()(ValueType v) {
while (v > m_nextLogged) {
m_out << m_nextLogged << '\t' << m_popped << '\n';
m_nextLogged *= LOG_STEP;
}
++m_popped;
return false;
}
};
class MaxMultiplicityLogger {
static constexpr Integral LOG_STEP = 2;
std::ofstream m_out;
Integral m_nextLogged = LOG_STEP;
Integral m_maxMultiplicity = 0;
public:
explicit MaxMultiplicityLogger(const std::string& filename) : m_out(filename, std::ios::out) {}
MaxMultiplicityLogger(const MaxMultiplicityLogger&) = default;
MaxMultiplicityLogger(MaxMultiplicityLogger&&) = default;
~MaxMultiplicityLogger() {
m_out.close();
}
template<typename ValueType>
bool operator()(WithMultiplicity<ValueType> v) {
while (v > m_nextLogged) {
m_out << m_nextLogged << '\t' << m_maxMultiplicity << '\n';
m_nextLogged *= LOG_STEP;
}
if (v.multiplicity > m_maxMultiplicity) {
m_maxMultiplicity = v.multiplicity;
}
return false;
}
};
class MaxDeltaLogger {
static constexpr Integral LOG_STEP = 2;
std::ofstream m_out;
Integral m_nextLogged = LOG_STEP;
Integral m_maxDelta = 0;
Integral m_prev = 0;
public:
explicit MaxDeltaLogger(const std::string& filename) : m_out(filename, std::ios::out) {}
MaxDeltaLogger(const MaxDeltaLogger&) = default;
MaxDeltaLogger(MaxDeltaLogger&&) = default;
~MaxDeltaLogger() {
m_out.close();
}
template<typename ValueType>
bool operator()(ValueType v) {
while (v > m_nextLogged) {
m_out << m_nextLogged << '\t' << m_maxDelta << '\n';
m_nextLogged *= LOG_STEP;
}
if (m_prev == 0 || v - m_prev > m_maxDelta) {
m_maxDelta = v - m_prev;
}
m_prev = static_cast<Integral>(v);
return false;
}
};
template<typename MyQueue = Queue0<Integral, 3, WithMultiplicity<Integral>>, std::size_t ValueLimit = 10'000'000'000>
void main6(Integral a2 = 0, Integral a3 = 2, Integral a6 = 3) {
std::string id = "_";
id += std::to_string(MyQueue::initValue) + "_";
id += std::to_string(a2) + "_" + std::to_string(a3) + "_" + std::to_string(a6);
MyQueue queue(std::make_pair(2, a2), std::make_pair(3, a3), std::make_pair(6, a6));
Processor processor(//Serializer(std::string("binary") + id),
DensityLogger(std::string("popped") + id),
MaxMultiplicityLogger(std::string("max_multiplicity") + id),
MaxDeltaLogger(std::string("max_delta") + id));
processor.Process<MyQueue, ValueLimit>(queue);
}
template<void (*func)(Integral, Integral, Integral)>
void main7() {
auto startTimePoint = std::chrono::system_clock::now();
func(0, 2, 3);
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
func(0, 3, 10);
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
func(2, 0, 15);
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
func(1, 0, 2);
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
func(2, 1, 0);
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
func(1, 6, 0);
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
}
template<void (*func)(Integral, Integral, Integral)>
void main5() {
stxxl::config::get_instance()->check_initialized();
auto startTimePoint = std::chrono::system_clock::now();
std::vector<std::thread> threads;
threads.emplace_back(func, 0, 2, 3);
threads.emplace_back(func, 0, 3, 10);
threads.emplace_back(func, 2, 0, 15);
threads.emplace_back(func, 1, 0, 2);
threads.emplace_back(func, 2, 1, 0);
threads.emplace_back(func, 1, 6, 0);
for (auto& t : threads) {
if (t.joinable()) {
t.join();
}
}
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>((std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
}
void main8() {
main5<&main6<Queue0<Integral, 3, WithMultiplicity<Integral>, stxxl::queue<WithMultiplicity<Integral>>, 50>>>();
}
template<Integral FunctionsCount = 3>
void main9() {
SearchAndIterate<FunctionsCount>([](auto v) {
std::array<SafeIntegral, FunctionsCount> a;
for (size_t i = 0; i < FunctionsCount; ++i) {
a[i] = v[i];
}
do {
std::array<std::pair<SafeIntegral, Rational>, FunctionsCount> functions;
Rational prefixSum;
SafeIntegral lcm(1);
SafeIntegral gcd(0);
for (std::size_t i = 0; i < FunctionsCount; ++i) {
Rational m(a[i]);
functions[i] = {a[i], prefixSum * m};
lcm = LCM(lcm, functions[i].second.GetDenominator());
gcd = GCD(gcd, functions[i].second.GetNumerator());
prefixSum += 1 / m;
}
for (auto& [m, b] : functions) {
b /= Rational(gcd);
b *= Rational(lcm);
std::cout << m << "x + " << (SafeIntegral)b << '\t';
}
std::cout << '\n';
} while (std::next_permutation(a.begin(), a.end()));
});
}
template<std::size_t FunctionsCount = 4, typename MyQueue = Queue0<Integral , FunctionsCount, WithMultiplicity<Integral>>, std::size_t ValueLimit = 10'000'000'000>
void ProcessFunctionSet(const std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount>& functions) {
std::string id = "_";
id += std::to_string(MyQueue::initValue);
for (auto [a, b] : functions) {
id += "_";
id += std::to_string(a.toIntegral()) + "_" + std::to_string(b.toIntegral());
}
MyQueue queue(functions);
Processor processor(//Serializer(std::string("binary") + id),
DensityLogger(std::string("popped") + id),
MaxMultiplicityLogger(std::string("max_multiplicity") + id),
MaxDeltaLogger(std::string("max_delta") + id));
processor.Process<MyQueue, ValueLimit>(queue);
}
template<Integral FunctionsCount = 3, std::size_t Skip = 0, std::size_t ValueLimit = 1'000'000'000, bool CalculateTimings = false, std::size_t ThreadsCount = 6>
void IterateAllFunctionSetsAsync(std::function<void(
const std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount> &)> func = ProcessFunctionSet<FunctionsCount, Queue0<Integral, FunctionsCount, WithMultiplicity<Integral>>, ValueLimit>) {
stxxl::config::get_instance()->check_initialized();
SearchAndIterate<FunctionsCount>([skip = Skip, &func](auto v) mutable {
if (skip != 0) {
std::cout << skip << " left to Skip. Skipping...\n";
--skip;
return;
}
std::array<std::thread, ThreadsCount> threads;
auto startTimePoint = std::chrono::system_clock::now();
std::array<SafeIntegral, FunctionsCount> a;
for (size_t i = 0; i < FunctionsCount; ++i) {
a[i] = v[i];
}
std::size_t i = 0;
do {
std::array<std::pair<SafeIntegral, Rational>, FunctionsCount> functions;
Rational prefixSum;
SafeIntegral lcm(1);
SafeIntegral gcd(0);
for (std::size_t i = 0; i < FunctionsCount; ++i) {
Rational m(a[i]);
functions[i] = {a[i], prefixSum * m};
lcm = LCM(lcm, functions[i].second.GetDenominator());
gcd = GCD(gcd, functions[i].second.GetNumerator());
prefixSum += 1 / m;
}
std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount> out;
for (std::size_t i = 0; i < FunctionsCount; ++i) {
functions[i].second /= Rational(gcd);
functions[i].second *= Rational(lcm);
out[i] = {functions[i].first, static_cast<SafeIntegral>(functions[i].second)};
}
std::sort(out.begin(), out.end());
i %= ThreadsCount;
if (threads[i].joinable()) {
threads[i].join();
}
threads[i++] = std::thread(func, out);
} while (std::next_permutation(a.begin(), a.end()));
for (auto& thread : threads) {
if (thread.joinable()) {
thread.join();
}
}
if (CalculateTimings) {
std::cout << "% " << std::chrono::duration_cast<std::chrono::milliseconds>(
(std::chrono::system_clock::now() - startTimePoint)).count() << " ms" << std::endl;
}
});
}
template<std::size_t FunctionsCount>
std::ostream &
operator<<(std::ostream &out, const std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount> &functionSet) {
for (auto& [a, b] : functionSet) {
out << a << "x + " << b << '\t';
}
return out;
}
template<bool NeedToProcess = true, std::size_t FunctionsCount = 5, std::size_t ValueLimit = 1'000'000'000, typename QueueType = std::queue<WithMultiplicity<Integral>>, bool CalculateTimings = true, std::size_t ThreadsCount = 4>
void HandleOutput() {
std::vector<std::pair<double, std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount>>> functionSets;
std::mutex mutex;
IterateAllFunctionSetsAsync<FunctionsCount, 0, ValueLimit, CalculateTimings, ThreadsCount>(
[&functionSets, &mutex](
const std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount> &functionSet) {
if (NeedToProcess) {
ProcessFunctionSet<FunctionsCount, Queue0<Integral, FunctionsCount, WithMultiplicity<Integral>, QueueType>, ValueLimit>(
functionSet);
}
std::string id = "popped_1";
for (auto[a, b]: functionSet) {
id += "_";
id += std::to_string(a.toIntegral()) + "_" + std::to_string(b.toIntegral());
}
std::ifstream fIn(id);
Integral prefix, popped;
double density;
while (fIn >> prefix >> popped) {
density = static_cast<double>(popped) / static_cast<double>(prefix);
}
std::scoped_lock lock(mutex);
functionSets.emplace_back(density, functionSet);
});
std::sort(functionSets.begin(), functionSets.end());
constexpr std::size_t topLimit = 10;
for (size_t i = 0; i < topLimit; ++i) {
std::cout << functionSets[i].second << functionSets[i].first << '\n';
}
std::cout << "...\n";
for (size_t i = 0; i < topLimit; ++i) {
std::cout << functionSets[functionSets.size() - topLimit + i].second
<< functionSets[functionSets.size() - topLimit + i].first << '\n';
}
}
template<std::size_t FunctionsCount = 4>
class MultiplicityChecker {
const std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount>& m_functions;
bool m_success = true;
public:
explicit MultiplicityChecker(const std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount> &functions)
: m_functions(functions) {}
MultiplicityChecker(MultiplicityChecker && other) noexcept : m_functions(other.m_functions) {
other.m_success = false;
}
~MultiplicityChecker() {
if (m_success) {
std::stringstream output;
output << "No multiplicity for " << m_functions << std::endl;
std::cout << output.str();
}
}
template<typename ValueType>
bool operator()(WithMultiplicity<ValueType> v) {
if (v.multiplicity > 1) {
m_success = false;
return true;
} else {
return false;
}
}
};
template<std::size_t FunctionsCount = 5, typename... F>
class Callbacks {
std::tuple<F...> m_callbacks;
public:
explicit Callbacks(const std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount> &functions)
: m_callbacks(F(functions)...) {}
Callbacks(Callbacks &&other) noexcept = default;
template<typename ValueType>
bool operator()(ValueType v) {
bool res = false;
std::initializer_list<int> _ = {(res = std::get<F>(m_callbacks)(v) || res, 42)...};
return res;
}
};
template<std::size_t FunctionsCount = 4, typename MyQueue = Queue0<Integral , FunctionsCount, WithMultiplicity<Integral>, std::queue<WithMultiplicity<Integral>>>, std::size_t ValueLimit = 1'000'000'000, typename CallbackType = MultiplicityChecker<FunctionsCount>>
void ProcessWithCallback(const std::array<std::pair<SafeIntegral, SafeIntegral>, FunctionsCount>& functions) {
MyQueue queue(functions);
Processor<CallbackType> processor((CallbackType(functions)));
processor.template Process<MyQueue, ValueLimit>(queue);
}
template<std::size_t... FunctionsCount>
void SearchForFunctionSetWithoutMultiplicity() {
std::initializer_list<int> _ = {
(IterateAllFunctionSetsAsync<FunctionsCount, 0, 10'000'000'000, false, 8>(ProcessWithCallback<FunctionsCount, Queue0<Integral, FunctionsCount, WithMultiplicity<Integral>>>), 42)...};
}
int main() {
SearchForFunctionSetWithoutMultiplicity<4, 5>();
}