結果

問題 No.93 ペガサス
ユーザー mayoko_mayoko_
提出日時 2015-09-09 13:55:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 2,530 bytes
コンパイル時間 635 ms
コンパイル使用メモリ 78,340 KB
実行使用メモリ 22,624 KB
最終ジャッジ日時 2023-09-26 10:14:41
合計ジャッジ時間 2,149 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 8 ms
7,296 KB
testcase_05 AC 4 ms
5,120 KB
testcase_06 AC 3 ms
4,588 KB
testcase_07 AC 21 ms
15,004 KB
testcase_08 AC 11 ms
9,388 KB
testcase_09 AC 34 ms
22,236 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 28 ms
19,532 KB
testcase_13 AC 10 ms
8,716 KB
testcase_14 AC 5 ms
5,712 KB
testcase_15 AC 25 ms
18,048 KB
testcase_16 AC 6 ms
6,356 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 34 ms
22,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 1003;
const ll MOD = 1e9+7;
ll dp[MAXN][MAXN][2][2]; // n, ng, (n-3,n-1), (n-2, n) -> num

inline void add(ll& a, ll b) {
    (a += b) %= MOD;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    if (N <= 2) {
        cout << N << endl;
        return 0;
    }
    dp[2][0][0][0] = 2;
    for (int n = 2; n < N; n++) for (int ng = 0; ng < n; ng++) {
        // (n-3, n-1) (n-2, n)
        ll v = dp[n][ng][1][1];
        if (v) {
            // split n-2, n
            add(dp[n+1][ng-1][0][0], v);
            // split n-3, n-1
            add(dp[n+1][ng][1][1], v);
            // adjacent to n-1
            add(dp[n+1][ng+1][1][1], v);
            // split others
            add(dp[n+1][ng-1][1][0], v*(ng-2));
            // others
            add(dp[n+1][ng][1][0], v*((n+1)-(ng+1)));
        }
        // (n-3, n-1)
        v = dp[n][ng][1][0];
        if (v) {
            // split n-3, n-1
            add(dp[n+1][ng][0][1], v);
            // adjacent to n-1
            add(dp[n+1][ng+1][0][1], v);
            // split others
            add(dp[n+1][ng-1][0][0], v*(ng-1));
            // others
            add(dp[n+1][ng][0][0], v*((n+1)-(ng+1)));
        }
        // (n-2, n)
        v = dp[n][ng][0][1];
        if (v) {
            // split n-2, n
            add(dp[n+1][ng-1][0][0], v);
            // adjacent to n-1
            add(dp[n+1][ng+1][1][1], 2*v);
            // split others
            add(dp[n+1][ng-1][1][0], v*(ng-1));
            // others
            add(dp[n+1][ng][1][0], v*((n+1)-(ng+2)));
        }
        // none
        v = dp[n][ng][0][0];
        if (v) {
            // adjacent to n-1
            add(dp[n+1][ng+1][0][1], 2*v);
            // split
            if (ng > 0) add(dp[n+1][ng-1][0][0], v*ng);
            // others
            add(dp[n+1][ng][0][0], v*((n+1)-(ng+2)));
        }
    }
    cout << dp[N][0][0][0] << endl;
    return 0;
}
0