結果

問題 No.1879 How many matchings?
ユーザー auauaauaua
提出日時 2022-03-18 22:56:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,313 bytes
コンパイル時間 1,844 ms
コンパイル使用メモリ 172,760 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 11:06:04
合計ジャッジ時間 2,672 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include <unordered_set>
#include <random>
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=INF;
const ll MAX=100010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;
double pi=M_PI;

//行列累乗
mat mul(mat &A,mat &B){
    mat C(A.size(),vec(B[0].size()));
    rep(i,A.size()){
        rep(j,B[0].size()){
            rep(k,B.size()){
                C[i][j]=(C[i][j]+(A[i][k]*B[k][j])%mod)%mod;
            }
        }
    }
    return C;
}
mat pow(mat A,ll k){
    mat B(A.size(),vec(A.size()));
    rep(i,A.size())B[i][i]=1;
    while(k>0){
        if(k&1)B=mul(B,A);
        A=mul(A,A);
        k>>=1;
    }
    return B;
}

void solve(){
    ll n;cin>>n;
    if(n==1){
        cout<<1<<endl;
    }else if(n==2){
        cout<<1<<endl;
    }else if(n==3){
        cout<<3<<endl;
    }else if(n==4){
        cout<<2<<endl;
    }else{
        if(n%2){
            vector<vector<ll> >k(4,vector<ll>(4));
            k[0][0]=2,k[0][1]=1,k[0][2]=-2,k[0][3]=-1;
            rep(j,3){
                k[j+1][j]=1;
            }
            vector<ll>a(4);
            a[0]=15;
            a[1]=7;
            a[2]=3;
            a[3]=1;
            ll ans=0;
            if(n/2>=4){
                auto c=pow(k,n/2-3);
                rep(j,4){
                    ans=(ans+a[j]*c[0][j]%mod)%mod;
                }
                if(ans<0)ans+=mod;
            }else{
                ans=a[3-n/2];
            }
            cout<<ans<<endl;
        }else{
            vector<vector<ll> >k(2,vector<ll>(2));
            k[0][0]=1,k[0][1]=1,k[1][0]=1;
            auto c=pow(k,n/2);
            cout<<c[0][0]<<endl;
        }
    }
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}
0