結果

問題 No.93 ペガサス
ユーザー antaanta
提出日時 2015-09-26 20:29:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 52 ms / 5,000 ms
コード長 4,664 bytes
コンパイル時間 649 ms
コンパイル使用メモリ 80,272 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 16:38:47
合計ジャッジ時間 2,095 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 9 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 29 ms
4,380 KB
testcase_08 AC 14 ms
4,376 KB
testcase_09 AC 52 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 43 ms
4,380 KB
testcase_13 AC 13 ms
4,380 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 37 ms
4,376 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 52 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }


template<int MOD>
struct ModInt {
	static const int Mod = MOD;
	unsigned x;
	ModInt() : x(0) {}
	ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }

	ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }

	ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
};
typedef ModInt<1000000007> mint;

struct DP {
	int N;
	vector<mint> dp;
	DP(int N_) : N(N_) { init(); }
	void init() {
		dp.assign(N * 8, mint());
	}
	void swap(DP &that) {
		dp.swap(that.dp);
	}
	//lasta: 1つ前のがattackされてるか
	//snda: 2つ前のがattackされてるか
	//twoc: 1つ前のと2つ前のが隣り合ってるか
	//num: 3つより前のattackされてる個数
	mint &operator()(int lasta, int snda, int twoc, int num) {
		return dp[(lasta * 4 + snda * 2 + twoc) * N + num];
	}
};

int main() {
	/*
	rer(N, 1, 10) {
		vi perm(N);
		iota(perm.begin(), perm.end(), 0);
		int cnt[2][2][2][11] = {};
		do {
			int lasta = N >= 3 && abs(perm[0] - perm[2]) == 1;
			int snda = N >= 4 && abs(perm[1] - perm[3]) == 1;
			int twoc = N >= 2 && abs(perm[0] - perm[1]) == 1;
			int num = 0;
			reu(i, 2, N - 2)
				num += abs(perm[i] - perm[i + 2]) == 1;
			++ cnt[lasta][snda][twoc][num];
		} while(next_permutation(perm.begin(), perm.end()));
		rep(lasta, 2) rep(snda, 2) rep(twoc, 2) rer(num, 0, N) {
			mint x = cnt[lasta][snda][twoc][num];
			if(x.x == 0) continue;
			cerr << N << ", " << lasta << ", " << snda << ", " << twoc << ", " << num << ": " << x.get() << endl;
		}
		int naiveans = cnt[0][0][0][0] + cnt[0][0][1][0];
		cerr << N << ": " << naiveans << endl;
	}*/

	int N;
	while(~scanf("%d", &N)) {
		DP dp(N), ndp(N);
		ndp(0, 0, 0, 0) = 1;
		rep(i, N) {
			dp.swap(ndp);
			ndp.init();
			rep(lasta, 2) rep(snda, 2) rep(twoc, 2) rer(num, 0, i) {
				mint x = dp(lasta, snda, twoc, num);
				if(x.x == 0) continue;
//				cerr << i << ", " << lasta << ", " << snda << ", " << twoc << ", " << num << ": " << x.get() << endl;
				if(twoc == 0) {
					if(i >= 1) {
						//1つ前のやつの左右
						ndp(0, lasta, 1, num + snda) += x;
						ndp(0, 0, 1, num + snda) += x;
					}
					if(i >= 2) {
						//2つ前のやつの左右
						ndp(1, lasta, 0, num) += x;
						ndp(1, lasta, 0, num + snda) += x;
					}
				} else {
					assert(i >= 2);
					//1つ前のやつの隣で、2つ前のじゃないほう
					ndp(0, 0, 1, num + snda) += x;
					//1つ前のと2つ前のとの間
					ndp(1, lasta, 1, num + snda) += x;
					//2つ前のやつの隣で、1つ前のじゃないほう
					ndp(1, lasta, 0, num) += x;
				}
				//1つ前の隣でも2つ前の隣でもない場所
				int spaces = (i + 1) - (i >= 1 ? 2 : 0) - (i >= 2 ? 2 : 0) + twoc;
				//そのうちattackされてるやつらの間
				if(num > 0)
					ndp(0, lasta, 0, num - 1 + snda) += x * num;
				//それ以外
				ndp(0, lasta, 0, num + snda) += x * (spaces - num);
			}
		}
		mint ans;
		rep(twoc, 2)
			ans += ndp(0, 0, twoc, 0);
		printf("%d\n", ans.get());
	}
	return 0;
}
0