結果

問題 No.140 みんなで旅行
ユーザー yyyuuuummmmaaa1yyyuuuummmmaaa1
提出日時 2019-08-01 16:46:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 960 ms / 5,000 ms
コード長 4,075 bytes
コンパイル時間 1,514 ms
コンパイル使用メモリ 155,652 KB
実行使用メモリ 18,936 KB
最終ジャッジ日時 2023-09-18 17:26:08
合計ジャッジ時間 8,893 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
18,920 KB
testcase_01 AC 68 ms
18,616 KB
testcase_02 AC 136 ms
18,672 KB
testcase_03 AC 67 ms
18,732 KB
testcase_04 AC 69 ms
18,616 KB
testcase_05 AC 70 ms
18,616 KB
testcase_06 AC 72 ms
18,736 KB
testcase_07 AC 71 ms
18,680 KB
testcase_08 AC 72 ms
18,912 KB
testcase_09 AC 72 ms
18,604 KB
testcase_10 AC 73 ms
18,796 KB
testcase_11 AC 944 ms
18,668 KB
testcase_12 AC 113 ms
18,620 KB
testcase_13 AC 93 ms
18,748 KB
testcase_14 AC 948 ms
18,616 KB
testcase_15 AC 960 ms
18,808 KB
testcase_16 AC 391 ms
18,816 KB
testcase_17 AC 258 ms
18,936 KB
testcase_18 AC 755 ms
18,620 KB
testcase_19 AC 791 ms
18,744 KB
testcase_20 AC 179 ms
18,676 KB
testcase_21 AC 80 ms
18,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
	
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
	os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
#define WHATS(var) //cout<<#var<<"="<<var<<endl;
	


const int mod = 1000000007;
struct Mod {
public:
	int num;
	Mod() : Mod(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) {
		static_assert(mod<INT_MAX / 2, "mod is too big, please make num 'long long int' from 'int'");
	}
	Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	return Mod(a) / b;
}
Mod operator/=(Mod &a, const Mod b) {
	return a = a / b;
}

#define MAX_MOD_N 102400

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init(const int amax = MAX_MOD_N) {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < amax - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	return fact[a] * factinv[b] * factinv[a - b];
}
int MAX_N=556;
vector<vector<vector<ll>>>dp(2,vector<vector<ll>>(MAX_N+1,vector<ll>(2*MAX_N+1)));
int main() {
	ios::sync_with_stdio(false);
	init();
	int X;cin>>X;

	dp[0][0][0]=1;

	for(int i=1;i<=X;++i){
		int cur=i&1;
		int tar=1-cur;
		
		for(int ok=0;ok<=i;++ok){
			for(int ng=0;ng<=2*i;++ng){

				ll sum=0;
				sum+=dp[tar][ok][ng]*ok;
				if(ok)sum+=dp[tar][ok-1][ng+1]*(ng+1);

				if(ok)sum+=dp[tar][ok-1][ng];

				//ok ok
				sum+=dp[tar][ok][ng]*ok*(ok-1);

				//ok ng
				sum+=dp[tar][ok][ng]*ok*ng*2;

				//ok emp
				if(ng)sum+=dp[tar][ok][ng-1]*ok*2;

				//ng ng
				sum+=dp[tar][ok][ng]*ng*(ng-1);

				// ng emp
				if(ng)sum+=dp[tar][ok][ng-1]*(ng-1)*2;
				//emp emp
				if(ng>=2)sum+=dp[tar][ok][ng-2];

				dp[cur][ok][ng]=sum%mod;
				WHATS(cur);
				WHATS(ok)
				WHATS(ng)
				WHATS(dp[cur][ok][ng]);
			}
		}
		
		dp[tar]=vector<vector<ll>>(MAX_N+1,vector<ll>(2*MAX_N+1));
	}

	ll answer=0;
	WHATS(answer)
	for(int ok=1;ok<=MAX_N;++ok){
		answer+=dp[(X&1)][ok][0]%mod;
	}
	cout<<answer%mod<<endl;
	return 0;
}
0