結果

問題 No.93 ペガサス
ユーザー GOTKAKO
提出日時 2025-07-01 00:33:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 57 ms / 5,000 ms
コード長 3,662 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 206,060 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-07-01 00:33:25
合計ジャッジ時間 3,095 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

//入力が必ず-mod<a<modの時.
template<const int mod>
struct modint{ //mod変更が不可能.
    public:
    long long v = 0;
    static void setmod(int m){} //飾り.
    static constexpr long long getmod(){return mod;}
    modint(){v = 0;} modint(int a){v = a<0?a+mod:a;} modint(long long a){v = a<0?a+mod:a;}
    modint(unsigned int a){v = a;} modint(unsigned long long a){v = a;}
    long long val()const{return v;}
 
    modint &operator=(const modint &b) = default;
    modint operator+()const{return (*this);}
    modint operator-()const{return modint(0)-(*this);}
    modint operator+(const modint b)const{return modint(v)+=b;}
    modint operator-(const modint b)const{return modint(v)-=b;}
    modint operator*(const modint b)const{return modint(v)*=b;}
    modint operator/(const modint b)const{return modint(v)/=b;}
    modint operator+=(const modint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    modint operator-=(const modint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    modint operator*=(const modint b){v = v*b.v%mod; return *this;}
    modint operator/=(modint b){ //b!=0 mod素数が必須.
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }
    modint operator++(){*this += 1; return *this;}
    modint operator--(){*this -= 1; return *this;}
    modint operator++(int){*this += 1; return *this;}
    modint operator--(int){*this -= 1; return *this;}
    bool operator==(const modint b)const{return v == b.v;}
    bool operator!=(const modint b)const{return v != b.v;}
    bool operator>(const modint b)const{return v > b.v;}
    bool operator>=(const modint b)const{return v >= b.v;}
    bool operator<(const modint b)const{return v < b.v;}
    bool operator<=(const modint b)const{return v <= b.v;}
    modint pow(long long n)const{
        modint ret = 1,p = v;
        if(n < 0) p = p.inv(),n = -n;
        while(n){
            if(n&1) ret *= p;
            p *= p; n >>= 1;
        }
        return ret;
    }
    modint inv()const{return modint(1)/v;} //素数mod必須.
};
using mint = modint<1000000007>;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    if(N == 1){
        cout << 1 << endl;
        return 0;
    }

    vector dp(N+1,vector<mint>(2));
    dp.at(1).at(0) = 1;
    for(int i=1; i<N/2; i++){
        vector next(N+1,vector<mint>(2));
        for(int k=0; k<N; k++) for(int l=0; l<2; l++){
            if(l) next.at(k+1).at(0) += dp.at(k).at(l);
            else next.at(k+1).at(0) += dp.at(k).at(l);  
            if(l == 0) next.at(k).at(1) += dp.at(k).at(l)*2;
            else next.at(k).at(1) += dp.at(k).at(l);
        }
        swap(dp,next);
    }
    {
        vector next(N+1,vector<mint>(2));
        for(int k=0; k<N; k++) for(int l=0; l<2; l++) next.at(k+1).at(0) += dp.at(k).at(l);
        swap(dp,next);
    }
    for(int i=1; i<(N+1)/2; i++){
        vector next(N+1,vector<mint>(2));
        for(int k=0; k<N; k++) for(int l=0; l<2; l++){
            if(l) next.at(k+1).at(0) += dp.at(k).at(l);
            else next.at(k+1).at(0) += dp.at(k).at(l);  
            if(l == 0) next.at(k).at(1) += dp.at(k).at(l)*2;
            else next.at(k).at(1) += dp.at(k).at(l);
        }
        swap(dp,next);
    }

    mint fac = 1,answer = 0;
    for(int i=1; i<=N; i++){
        fac *= i;
        mint v = dp.at(i).at(0)+dp.at(i).at(1);
        if((N-i)%2) answer -= v*fac;
        else answer += v*fac;
    }
    cout << answer.v << endl;
}
0