-
Notifications
You must be signed in to change notification settings - Fork 4
/
DIV_1_Q2_FIXES
314 lines (286 loc) · 8.92 KB
/
DIV_1_Q2_FIXES
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
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using str = string; // yay python!
// pairs
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
#define mp make_pair
#define f first
#define s second
#define tcT template <class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT > using V = vector<T>;
tcT, size_t SZ > using AR = array<T, SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
tcT > int lwb(V<T> &a, const T &b) { return int(lb(all(a), b) - bg(a)); }
tcT > int upb(V<T> &a, const T &b) { return int(ub(all(a), b) - bg(a)); }
// loops
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define each(a, x) for (auto &a : x)
const int MOD = 998244353; // 1e9+7;
const int MX = (int)2e5 + 5;
const ll BIG = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1}; // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
// bitwise ops
// also see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { // assert(x >= 0); // make C++11 compatible until
// USACO updates ...
return x == 0 ? 0 : 31 - __builtin_clz(x);
} // floor(log2(x))
constexpr int p2(int x) { return 1 << x; }
constexpr int msk2(int x) { return p2(x) - 1; }
ll cdiv(ll a, ll b) {
return a / b + ((a ^ b) > 0 && a % b);
} // divide a by b rounded up
ll fdiv(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b);
} // divide a by b rounded down
tcT > bool ckmin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT > bool ckmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
} // set a = max(a,b)
tcTU > T fstTrue(T lo, T hi, U f) {
++hi;
assert(lo <= hi); // assuming f is increasing
while (lo < hi) { // find first index such that f is true
T mid = lo + (hi - lo) / 2;
f(mid) ? hi = mid : lo = mid + 1;
}
return lo;
}
tcTU > T lstTrue(T lo, T hi, U f) {
--lo;
assert(lo <= hi); // assuming f is decreasing
while (lo < hi) { // find first index such that f is true
T mid = lo + (hi - lo + 1) / 2;
f(mid) ? lo = mid : hi = mid - 1;
}
return lo;
}
tcT > void remDup(vector<T> &v) { // sort and remove duplicates
sort(all(v));
v.erase(unique(all(v)), end(v));
}
tcTU > void safeErase(T &t, const U &u) {
auto it = t.find(u);
assert(it != end(t));
t.erase(it);
}
inline namespace IO {
#define SFINAE(x, ...) \
template <class, class = void> struct x : std::false_type {}; \
template <class T> struct x<T, std::void_t<__VA_ARGS__>> : std::true_type {}
SFINAE(DefaultI, decltype(std::cin >> std::declval<T &>()));
SFINAE(DefaultO, decltype(std::cout << std::declval<T &>()));
SFINAE(IsTuple, typename std::tuple_size<T>::type);
SFINAE(Iterable, decltype(std::begin(std::declval<T>())));
template <auto &is> struct Reader {
template <class T> void Impl(T &t) {
if constexpr (DefaultI<T>::value) is >> t;
else if constexpr (Iterable<T>::value) {
for (auto &x : t) Impl(x);
} else if constexpr (IsTuple<T>::value) {
std::apply([this](auto &...args) { (Impl(args), ...); }, t);
} else static_assert(IsTuple<T>::value, "No matching type for read");
}
template <class... Ts> void read(Ts &...ts) { ((Impl(ts)), ...); }
};
template <class... Ts> void re(Ts &...ts) { Reader<cin>{}.read(ts...); }
#define def(t, args...) \
t args; \
re(args);
template <auto &os, bool debug, bool print_nd> struct Writer {
string comma() const { return debug ? "," : ""; }
template <class T> constexpr char Space(const T &) const {
return print_nd && (Iterable<T>::value or IsTuple<T>::value) ? '\n'
: ' ';
}
template <class T> void Impl(T const &t) const {
if constexpr (DefaultO<T>::value) os << t;
else if constexpr (Iterable<T>::value) {
if (debug) os << '{';
int i = 0;
for (auto &&x : t)
((i++) ? (os << comma() << Space(x), Impl(x)) : Impl(x));
if (debug) os << '}';
} else if constexpr (IsTuple<T>::value) {
if (debug) os << '(';
std::apply(
[this](auto const &...args) {
int i = 0;
(((i++) ? (os << comma() << " ", Impl(args)) : Impl(args)),
...);
},
t);
if (debug) os << ')';
} else static_assert(IsTuple<T>::value, "No matching type for print");
}
template <class T> void ImplWrapper(T const &t) const {
if (debug) os << "\033[0;31m";
Impl(t);
if (debug) os << "\033[0m";
}
template <class... Ts> void print(Ts const &...ts) const {
((Impl(ts)), ...);
}
template <class F, class... Ts>
void print_with_sep(const std::string &sep, F const &f,
Ts const &...ts) const {
ImplWrapper(f), ((os << sep, ImplWrapper(ts)), ...), os << '\n';
}
void print_with_sep(const std::string &) const { os << '\n'; }
};
template <class... Ts> void pr(Ts const &...ts) {
Writer<cout, false, true>{}.print(ts...);
}
template <class... Ts> void ps(Ts const &...ts) {
Writer<cout, false, true>{}.print_with_sep(" ", ts...);
}
} // namespace IO
inline namespace Debug {
template <typename... Args> void err(Args... args) {
Writer<cerr, true, false>{}.print_with_sep(" | ", args...);
}
template <typename... Args> void errn(Args... args) {
Writer<cerr, true, true>{}.print_with_sep(" | ", args...);
}
void err_prefix(str func, int line, string args) {
cerr << "\033[0;31m\u001b[1mDEBUG\033[0m"
<< " | "
<< "\u001b[34m" << func << "\033[0m"
<< ":"
<< "\u001b[34m" << line << "\033[0m"
<< " - "
<< "[" << args << "] = ";
}
#ifdef LOCAL
#define dbg(args...) err_prefix(__FUNCTION__, __LINE__, #args), err(args)
#define dbgn(args...) err_prefix(__FUNCTION__, __LINE__, #args), errn(args)
#else
#define dbg(...)
#define dbgn(args...)
#endif
const auto beg_time = std::chrono::high_resolution_clock::now();
// https://stackoverflow.com/questions/47980498/accurate-c-c-clock-on-a-multi-core-processor-with-auto-overclock?noredirect=1&lq=1
double time_elapsed() {
return chrono::duration<double>(std::chrono::high_resolution_clock::now() -
beg_time)
.count();
}
} // namespace Debug
inline namespace FileIO {
void setIn(str s) { freopen(s.c_str(), "r", stdin); }
void setOut(str s) { freopen(s.c_str(), "w", stdout); }
void setIO(str s = "") {
cin.tie(0)->sync_with_stdio(0); // unsync C / C++ I/O streams
cout << fixed << setprecision(12);
// cin.exceptions(cin.failbit);
// throws exception when do smth illegal
// ex. try to read letter into int
if (sz(s)) setIn(s + ".in"), setOut(s + ".out"); // for old USACO
}
} // namespace FileIO
// make sure to intialize ALL GLOBAL VARS between tcs!
V<db> precomp[5001];
void solve(int tc) {
def(int, N, M);
V<vi> out(N + 1);
V<db> ok(N + 1);
rep(M) {
def(int, a, b);
out[a].pb(b);
}
ok[N] = 1;
ROF(i, 1, N) {
V<db> probs;
for (int x : out[i]) probs.pb(ok[x]);
sort(rall(probs));
R0F(j, sz(probs)) ok[i] += precomp[sz(probs)].at(j) * probs[j];
}
ps(ok[1]);
}
int main() {
setIO();
precomp[1] = {1};
FOR(n, 2, 5001) {
precomp[n].rsz(n);
precomp[n][0] = 1. / n;
FOR(r, 1, n) {
if (r > 1)
precomp[n][r] += db(r - 1) / n * precomp[n - 2].at(r - 2);
if (r + 1 < n)
precomp[n][r] += db(n - r - 1) / n * precomp[n - 2].at(r - 1);
}
// FOR(r, 1, n) {
// int idx = 0;
// FOR(j, 1, n) if (j != r) {
// precomp[n][j] += precomp[n - 2].at(idx) / n;
// ++idx;
// }
// }
if (n <= 10) dbg(n, precomp[n]);
}
int TC;
re(TC);
FOR(i, 1, TC + 1) solve(i);
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/