結果

問題 No.93 ペガサス
ユーザー krotonkroton
提出日時 2014-12-05 08:11:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 1,691 bytes
コンパイル時間 440 ms
コンパイル使用メモリ 63,940 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-02 08:51:33
合計ジャッジ時間 1,816 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;

const ll MOD = 1e9 + 7;
ll dp[2][1050][2][2];

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

ll solve(int n){
    if(n == 1)return 1;
    
    int curr = 0, next = 1;
    memset(dp[curr], 0, sizeof(dp[curr]));
    
	int segs = 3;
    dp[curr][0][0][0] = 2;

    for(int i=3;i<=n;i++){
        memset(dp[next], 0, sizeof(dp[next]));
        
        for(int b=0;b<=segs-2;b++){
            int s = segs - b;
            
            for(int p2=0;p2<=1;p2++)for(int p1=0;p1<=1;p1++){
                if(b < p1 + p2)continue;

                ll now = dp[curr][b][p2][p1];
                int nb = b;
                int ns = s;

                if(p2 == 0){
                    add(dp[next][b+1][p1][1], now * 2);
                    ns -= 2;
                } else {
                    add(dp[next][b+1][p1][1], now);
                    add(dp[next][b][p1][1], now);
                    ns -= 1;
                    nb -= 1;
                }

                if(p1 == 1){
                    add(dp[next][b-1][0][0], now);
                    nb -= 1;
                }

                if(nb > 0){
                    add(dp[next][b-1][p1][0], now * nb);
                }

                if(ns > 0){
                    add(dp[next][b][p1][0], now * ns);
                }
            }
        }
        
        swap(curr, next);
        ++segs;
    }
    
    return dp[curr][0][0][0];
}


int main(){
    int N;
    cin >> N;
    
    cout << solve(N) << endl;
    return 0;
}
0