Submission #497688

# Submission time Handle Problem Language Result Execution time Memory
497688 2021-12-23T15:04:24 Z amoo_safar Ruins 3 (JOI20_ruins3) C++17
100 / 100
586 ms 336 KB
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

const int N = 6e2 + 10;


namespace ModularInt {
	template<typename T>
	T inverse(T a, T pr){
		T u = 1, v = 0;
		while(a != 0){
			T dv = pr / a;
			pr -= dv * a; v -= dv * u;
			swap(a, pr); swap(u, v);
		}
		assert(pr == 1); // gcd(a, pr) != 1;
		return v;
	}

	template<typename ModT>
	class Mint {
	public:
		using T = typename decay<decltype(ModT::value)>::type;
		T val;
	

		Mint() : val() {}
		template<typename Q>
		Mint(const Q& x){ val = normalize(x); }
		Mint(const Mint& x) { val = x.val; }

		template <typename U>
		explicit operator U() const { return static_cast<U>(val); }

		constexpr static T _Mod() { return ModT::value; }

		friend Mint inverse(Mint x){ return inverse(x.val, _Mod()); }

		template<typename Q>
		static T normalize(const Q& x){
			T val;
			if(-_Mod() <= x && x < _Mod()) val = x;
			else val = x % _Mod();
			if(val < 0) val += _Mod();
			return val;
		}
		
		template<typename Q>
		Mint& operator=(const Q& x){ val = normalize(x); return *this; }

		Mint& operator +=(const Mint& other){ if(_Mod() <= (val += other.val)) val -= _Mod(); return *this; }
		Mint& operator -=(const Mint& other){ if((val -= other.val) < 0) val += _Mod(); return *this; }
		template<typename Q> Mint& operator +=(const Q& other){ return (*this) += Mint(other); };
		template<typename Q> Mint& operator -=(const Q& other){ return (*this) -= Mint(other); };
		Mint& operator ++(){ return (*this) += 1; }
		Mint& operator --(){ return (*this) -= 1; }
		Mint operator ++(int){ Mint res(*this); (*this) += 1; return res; }
		Mint operator --(int){ Mint res(*this); (*this) -= 1; return res; }
		Mint operator +(){ Mint res(val); return res; }
		Mint operator -(){ Mint res(-val); return res; }

		Mint& operator *=(const Mint& other){ val = (1ll * val * other.val) % _Mod(); return *this; }
		Mint& operator /=(const Mint& other){ return (*this) *= inverse(other); }
		template<typename Q> Mint& operator *=(const Q& other){ return (*this) *= Mint(other); }
		template<typename Q> Mint& operator /=(const Q& other){ return (*this) /= Mint(other); }
	};


	template<typename T> Mint<T> operator +(const Mint<T>& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) += rhs; }
	template<typename T, typename Q>  Mint<T> operator +(const Q& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) += rhs; }
	template<typename T, typename Q>  Mint<T> operator +(const Mint<T>& lhs, const Q& rhs){ return Mint<T>(lhs) += rhs; }

	template<typename T> Mint<T> operator -(const Mint<T>& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) -= rhs; }
	template<typename T, typename Q>  Mint<T> operator -(const Q& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) -= rhs; }
	template<typename T, typename Q>  Mint<T> operator -(const Mint<T>& lhs, const Q& rhs){ return Mint<T>(lhs) -= rhs; }

	template<typename T> Mint<T> operator *(const Mint<T>& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) *= rhs; }
	template<typename T, typename Q>  Mint<T> operator *(const Q& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) *= rhs; }
	template<typename T, typename Q>  Mint<T> operator *(const Mint<T>& lhs, const Q& rhs){ return Mint<T>(lhs) *= rhs; }

	template<typename T> Mint<T> operator /(const Mint<T>& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) /= rhs; }
	template<typename T, typename Q>  Mint<T> operator /(const Q& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) /= rhs; }
	template<typename T, typename Q>  Mint<T> operator /(const Mint<T>& lhs, const Q& rhs){ return Mint<T>(lhs) /= rhs; }

	template<typename T> bool operator ==(const Mint<T>& lhs, const Mint<T>& rhs){ return lhs.val == rhs.val; }
	template<typename T, typename Q>  bool operator ==(const Q& lhs, const Mint<T>& rhs){ return Mint<T>(lhs) == rhs; }
	template<typename T, typename Q>  bool operator ==(const Mint<T>& lhs, const Q& rhs){ return lhs == Mint<T>(rhs); }

	template<typename T> bool operator !=(const Mint<T>& lhs, const Mint<T>& rhs){ return !(lhs == rhs); }
	template<typename T, typename Q>  bool operator !=(const Q& lhs, const Mint<T>& rhs){ return !(lhs == rhs); }
	template<typename T, typename Q>  bool operator !=(const Mint<T>& lhs, const Q& rhs){ return !(lhs == rhs); }

	template<typename T, typename IO>
	IO& operator <<(IO& ostream, const Mint<T>& _x){
		return ostream << _x.val;
	}
	template<typename T, typename IO>
	IO& operator >>(IO& istream, Mint<T>& _x){
		return istream >> _x.val;
	}

	template<typename T, typename Q>
	Mint<T> bin_pow(const Mint<T>& base, Q power){
		assert(0 <= power);
		Mint<T> res = 1, pw = base;
		for(Q j = 1; j <= power; j <<= 1, pw *= pw)
			if(j & power)
				res *= pw;
		return res;
	}

	// Dynamic
	
	/*
	using ModType = int;
	struct VarMod { static ModType value; };
	ModType VarMod::value;
	ModType& MintMod = VarMod::value;
	using mi = Mint<VarMod>;
	*/

	// Static
	constexpr int MintMod = 1'000'000'007;
	using mi = Mint<integral_constant<decay<decltype(MintMod)>::type, MintMod>>;
};

using namespace ModularInt;

mi dp[N];
mi per[N];
int mk[N + N];

mi f[N], iv[N];
mi nCr(int n, int r){
	if(n < 0 || r < 0 || n < r) return 0;
	return f[n] * iv[r] * iv[n - r];
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	f[0] = 1;
	for(int i = 1; i < N; i++) f[i] = i * f[i - 1];
	for(int i = 0; i < N; i++)iv[i] = inverse(f[i]);
	
	per[0] = 1;
	for(int i = 1; i < N; i++)
		for(int j = 0; j < i; j++)
			per[i] += nCr(i - 1, j) * per[j] * per[i - j - 1] * (i - j + 1);

	int n;
	cin >> n;
	int p;
	for(int i = 0; i < n; i++){
		cin >> p;
		mk[p] = 1;
	}
	dp[0] = 1;
	int c1 = 0, fr = 0;
	for(int _ = n + n; _; fr ++, _--){
		if(!mk[_]){
			for(int i = 0; i <= c1; i++) dp[i] *= max(0, i - fr + c1);
		} else {
			c1 ++;
			for(int i = c1; i >= 0; i--){
				for(int j = 0; j < i; j++){
					dp[i] += nCr((c1 - 1) - j, i - j - 1) * dp[j] * per[i - j - 1] * (i - j + 1);
				}
			}
		}
	}
	cout << dp[n] / bin_pow(mi(2), n) << '\n';
	return 0;
}
/*
10
2 8 9 13 15 16 17 18 19 20

*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 208 KB Output is correct
2 Correct 3 ms 208 KB Output is correct
3 Correct 4 ms 316 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
5 Correct 3 ms 208 KB Output is correct
6 Correct 3 ms 208 KB Output is correct
7 Correct 4 ms 336 KB Output is correct
8 Correct 3 ms 328 KB Output is correct
9 Correct 3 ms 336 KB Output is correct
10 Correct 3 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 208 KB Output is correct
2 Correct 3 ms 208 KB Output is correct
3 Correct 4 ms 316 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
5 Correct 3 ms 208 KB Output is correct
6 Correct 3 ms 208 KB Output is correct
7 Correct 4 ms 336 KB Output is correct
8 Correct 3 ms 328 KB Output is correct
9 Correct 3 ms 336 KB Output is correct
10 Correct 3 ms 328 KB Output is correct
11 Correct 4 ms 208 KB Output is correct
12 Correct 4 ms 208 KB Output is correct
13 Correct 4 ms 336 KB Output is correct
14 Correct 4 ms 336 KB Output is correct
15 Correct 4 ms 208 KB Output is correct
16 Correct 6 ms 208 KB Output is correct
17 Correct 4 ms 332 KB Output is correct
18 Correct 4 ms 332 KB Output is correct
19 Correct 4 ms 208 KB Output is correct
20 Correct 5 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 208 KB Output is correct
2 Correct 3 ms 208 KB Output is correct
3 Correct 4 ms 316 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
5 Correct 3 ms 208 KB Output is correct
6 Correct 3 ms 208 KB Output is correct
7 Correct 4 ms 336 KB Output is correct
8 Correct 3 ms 328 KB Output is correct
9 Correct 3 ms 336 KB Output is correct
10 Correct 3 ms 328 KB Output is correct
11 Correct 4 ms 208 KB Output is correct
12 Correct 4 ms 208 KB Output is correct
13 Correct 4 ms 336 KB Output is correct
14 Correct 4 ms 336 KB Output is correct
15 Correct 4 ms 208 KB Output is correct
16 Correct 6 ms 208 KB Output is correct
17 Correct 4 ms 332 KB Output is correct
18 Correct 4 ms 332 KB Output is correct
19 Correct 4 ms 208 KB Output is correct
20 Correct 5 ms 208 KB Output is correct
21 Correct 569 ms 328 KB Output is correct
22 Correct 577 ms 312 KB Output is correct
23 Correct 572 ms 328 KB Output is correct
24 Correct 568 ms 316 KB Output is correct
25 Correct 565 ms 332 KB Output is correct
26 Correct 575 ms 336 KB Output is correct
27 Correct 555 ms 328 KB Output is correct
28 Correct 556 ms 208 KB Output is correct
29 Correct 568 ms 336 KB Output is correct
30 Correct 567 ms 332 KB Output is correct
31 Correct 558 ms 312 KB Output is correct
32 Correct 586 ms 312 KB Output is correct
33 Correct 580 ms 316 KB Output is correct
34 Correct 580 ms 336 KB Output is correct
35 Correct 577 ms 312 KB Output is correct
36 Correct 561 ms 336 KB Output is correct