-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from NyaanNyaan/add_verify
add verify
- Loading branch information
Showing
21 changed files
with
886 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
using namespace std; | ||
|
||
template <typename T, typename F> | ||
struct SlideWindowAggregationDeque { | ||
vector<T> a0, a1, r0, r1; | ||
F f; | ||
T I; | ||
|
||
SlideWindowAggregationDeque(F _f, T _i) : f(_f), I(_i) {} | ||
|
||
private: | ||
T get0() const { return r0.empty() ? I : r0.back(); } | ||
T get1() const { return r1.empty() ? I : r1.back(); } | ||
|
||
void push0(const T &x) { | ||
a0.push_back(x); | ||
r0.push_back(f(x, get0())); | ||
} | ||
void push1(const T &x) { | ||
a1.push_back(x); | ||
r1.push_back(f(get1(), x)); | ||
} | ||
void rebalance() { | ||
int n = a0.size() + a1.size(); | ||
int s0 = n / 2 + (a0.empty() ? n % 2 : 0); | ||
vector<T> a{a0}; | ||
reverse(begin(a), end(a)); | ||
copy(begin(a1), end(a1), back_inserter(a)); | ||
a0.clear(), r0.clear(); | ||
a1.clear(), r1.clear(); | ||
for (int i = s0 - 1; i >= 0; i--) push0(a[i]); | ||
for (int i = s0; i < n; i++) push1(a[i]); | ||
} | ||
|
||
public: | ||
void push_front(const T &t) { push0(t); } | ||
void push_back(const T &t) { push1(t); } | ||
T front() const { return a0.empty() ? a1.front() : a0.back(); } | ||
T back() const { return a1.empty() ? a0.front() : a1.back(); } | ||
void pop_front() { | ||
if (a0.empty()) rebalance(); | ||
assert(!a0.empty()); | ||
a0.pop_back(), r0.pop_back(); | ||
} | ||
void pop_back() { | ||
if (a1.empty()) rebalance(); | ||
assert(!a1.empty()); | ||
a1.pop_back(), r1.pop_back(); | ||
} | ||
T query() { return f(get0(), get1()); } | ||
}; | ||
|
||
/** | ||
* @brief Slide Window Aggrigation (deque) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <algorithm> | ||
#include <cassert> | ||
#include <cmath> | ||
#include <numeric> | ||
#include <vector> | ||
using namespace std; | ||
|
||
#include "../misc/rng.hpp" | ||
|
||
// N 択, 報酬最大化 | ||
struct MultiArmedBandit { | ||
MultiArmedBandit(int n) | ||
: N(n), last(-1), iter(0), thres(N * 5), num(N), v(N), e(N), t(1) {} | ||
|
||
int N, last; | ||
long long iter, thres; | ||
vector<long long> num; | ||
vector<double> v, e; | ||
double t; | ||
|
||
int play() { | ||
assert(last == -1); | ||
iter++; | ||
if (iter <= thres) return last = iter % N; | ||
|
||
double s = accumulate(begin(e), end(e), 0.0); | ||
double x = rnd() * s; | ||
for (int i = 0; i < N; i++) { | ||
if ((x -= e[i]) <= 0) return last = i; | ||
} | ||
return last = N - 1; | ||
} | ||
|
||
// 重み付け用の関数 | ||
double f(double x) { return exp(x / t); } | ||
|
||
void reward(double y) { | ||
assert(last != -1); | ||
v[last] += y; | ||
num[last] += 1; | ||
e[last] = f(v[last] / num[last]); | ||
last = -1; | ||
|
||
static double u = 1.0; | ||
static double du = 0.01; | ||
// iter % thres == 0 になったら t を再決定 | ||
if (iter % thres == 0) { | ||
u = max(0.7, u - du); | ||
double average = accumulate(begin(v), end(v), 0.0) / thres; | ||
t = average < 0.0 ? 1.0 : pow(average, u); | ||
for (int i = 0; i < N; i++) e[i] = f(v[i] / num[i]); | ||
} | ||
} | ||
int best() { return max_element(begin(e), end(e)) - begin(e); } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#pragma once | ||
|
||
#include "rational-binomial.hpp" | ||
#include "rational-fps.hpp" | ||
#include "rational.hpp" | ||
// | ||
#include "bigint-rational.hpp" | ||
#include "bigint.hpp" | ||
// | ||
using mint = BigRational; | ||
using vm = vector<mint>; | ||
using vvm = vector<vm>; | ||
using fps = FormalPowerSeries_rational<mint>; | ||
Binomial_rational<mint> C; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
using namespace std; | ||
|
||
#include "../internal/internal-math.hpp" | ||
#include "../prime/fast-factorize.hpp" | ||
|
||
long long primitive_root_ll(long long p) { | ||
if (p == 2) return 1; | ||
auto fs = factorize(p - 1); | ||
sort(begin(fs), end(fs)); | ||
fs.erase(unique(begin(fs), end(fs)), end(fs)); | ||
for (int g = 2;; g++) { | ||
int ok = 1; | ||
for (auto& f : fs) { | ||
if (internal::modpow<long long, __int128_t>(g, (p - 1) / f, p) == 1) { | ||
ok = false; | ||
break; | ||
} | ||
} | ||
if (ok) return g; | ||
} | ||
exit(1); | ||
} |
Oops, something went wrong.