Submission #683349

# Submission time Handle Problem Language Result Execution time Memory
683349 2023-01-18T08:28:33 Z vovamr Energetic turtle (IZhO11_turtle) C++17
50 / 100
269 ms 23356 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) 	(x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }

const int N = 6e5 + 100;

int md;
inline int add(int a, int b) { if ((a += b) >= md) a -= md; return a; }
inline void ads(int &a, int b) { if ((a += b) >= md) a -= md; }
inline int sub(int a, int b) { if ((a -= b) < 0) a += md; return a; }
inline void subs(int &a, int b) { if ((a -= b) < 0) a += md; }
inline int mul(int a, int b) { return a * 1ll * b % md; }
inline void muls(int &a, int b) { a = a * 1ll * b % md; }
inline int bp(int a, int n) { int res = 1;
	for (; n; n >>= 1, muls(a, a)) {
		if (n & 1) muls(res, a);
	} return res;
}

int lp[N];
ve<int> pr;

int pw[N][20];

inline void calc() {
	for (int i = 2; i < N; ++i) {
		if (!lp[i]) lp[i] = i, pr.pb(i);
		for (int j = 0; j < sz(pr) && i * pr[j] < N && pr[j] <= lp[i]; ++j) {
			lp[i * pr[j]] = pr[j];
		}
	}

	for (int id = 0; id < sz(pr); ++id) {
		const int &p = pr[id];

		pw[id][0] = 1;
		for (int i = 1; i < 20; ++i) pw[id][i] = mul(pw[id][i - 1], p);
	}
}

inline int c(int n, int k) {
	int answer = 1;
	int cnt, f; int nk = n - k;
	for (int id = 0; id < sz(pr) && pr[id] <= n; ++id) {
		const int &p = pr[id]; cnt = 0;

		f = p;
		while (f <= n) cnt += n / f, f *= p;

		f = p;
		while (f <= k) cnt -= k / f, f *= p;

		f = p;
		while (f <= nk) cnt -= nk / f, f *= p;
		
		muls(answer, pw[id][cnt]);
	}
	return answer;
}

inline void solve() {

	int n, m, k, t;
	cin >> n >> m >> k >> t >> md;

	calc();

	ve<pii> pts(k);
	for (auto &[x, y] : pts) cin >> x >> y;

	pts.pb({0, 0}); pts.pb({n, m});

	sort(all(pts), [](const pii &a, const pii &b) {
		return a.fi + a.se < b.fi + b.se;
	});

	int s = sz(pts);
	ve<ve<int>> cost(s, ve<int> (s));

	for (int i = 0; i < s; ++i) {
		const auto &[x1, y1] = pts[i];
		for (int j = i + 1; j < s; ++j) {
			const auto [x2, y2] = pts[j];

			if (!(x2 >= x1 && y2 >= y1)) continue;
			cost[i][j] = c(x2 - x1 + y2 - y1, x2 - x1);
		}
	}

	ve<int> value(1 << s);
	for (int mask = 0; mask < (1 << s); ++mask) {
		if ((mask >> 0 & 1 ^ 1) || (mask >> (s - 1) & 1 ^ 1)) continue;

		int prev = 0, result = 1;
		for (int i = 1; i < s; ++i) {
			if (mask >> i & 1 ^ 1) continue;

			muls(result, cost[prev][i]);
			prev = i;
		}
		value[mask] = result;
	}
	for (int i = 0; i < (1 << s); ++i) if (__builtin_popcount(i) & 1) {
		value[i] = md - value[i];
	}

	for (int i = 0; i < s; ++i) {
		for (int ma = 0; ma < (1 << s); ++ma) {
			if (ma >> i & 1) continue;
			ads(value[ma], value[ma ^ (1 << i)]);
		}
	}
	for (int i = 0; i < (1 << s); ++i) if (__builtin_popcount(i) & 1) {
		value[i] = md - value[i];
	}

	int answer = 0;
	for (int mask = 0; mask < (1 << s); ++mask) {
		if ((mask >> 0 & 1 ^ 1) || (mask >> (s - 1) & 1 ^ 1)) continue;
		if (__builtin_popcount(mask) - 2 <= t) {
			ads(answer, value[mask]);
		}
	}

	cout << answer;
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int q = 1; // cin >> q;
	while (q--) solve();
	cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}

Compilation message

turtle.cpp: In function 'void solve()':
turtle.cpp:110:18: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  110 |   if ((mask >> 0 & 1 ^ 1) || (mask >> (s - 1) & 1 ^ 1)) continue;
      |        ~~~~~~~~~~^~~
turtle.cpp:110:47: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  110 |   if ((mask >> 0 & 1 ^ 1) || (mask >> (s - 1) & 1 ^ 1)) continue;
      |                               ~~~~~~~~~~~~~~~~^~~
turtle.cpp:114:18: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  114 |    if (mask >> i & 1 ^ 1) continue;
      |        ~~~~~~~~~~^~~
turtle.cpp:137:18: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  137 |   if ((mask >> 0 & 1 ^ 1) || (mask >> (s - 1) & 1 ^ 1)) continue;
      |        ~~~~~~~~~~^~~
turtle.cpp:137:47: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  137 |   if ((mask >> 0 & 1 ^ 1) || (mask >> (s - 1) & 1 ^ 1)) continue;
      |                               ~~~~~~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 6744 KB Output is correct
2 Correct 13 ms 6748 KB Output is correct
3 Correct 14 ms 6748 KB Output is correct
4 Correct 19 ms 7268 KB Output is correct
5 Correct 221 ms 23220 KB Output is correct
6 Correct 14 ms 6872 KB Output is correct
7 Correct 27 ms 7768 KB Output is correct
8 Correct 16 ms 6744 KB Output is correct
9 Correct 66 ms 10860 KB Output is correct
10 Correct 113 ms 14996 KB Output is correct
11 Incorrect 34 ms 7760 KB Output isn't correct
12 Incorrect 145 ms 15032 KB Output isn't correct
13 Incorrect 29 ms 6752 KB Output isn't correct
14 Incorrect 52 ms 8892 KB Output isn't correct
15 Incorrect 231 ms 23260 KB Output isn't correct
16 Incorrect 262 ms 23356 KB Output isn't correct
17 Incorrect 86 ms 10868 KB Output isn't correct
18 Incorrect 269 ms 23220 KB Output isn't correct
19 Incorrect 269 ms 23264 KB Output isn't correct
20 Incorrect 242 ms 23220 KB Output isn't correct