結果

問題 No.1879 How many matchings?
ユーザー monnumonnu
提出日時 2022-03-18 22:48:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,476 bytes
コンパイル時間 4,086 ms
コンパイル使用メモリ 229,436 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 10:53:43
合計ジャッジ時間 4,967 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll=long long;
#define MOD 998244353
using Graph=vector<vector<pair<int,int>>>;
#define INF 1000000000
#define MAX 200000

vector<vector<ll>> multi(vector<vector<ll>> a,vector<vector<ll>> b){
  int n=a.size();
  int m=b[0].size();
  int l=b.size();
  vector<vector<ll>> ret(n,vector<ll>(m,0));
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      for(int k=0;k<l;k++){
        ret[i][j]+=a[i][k]*b[k][j]%MOD;
      }
      ret[i][j]%=MOD;
    }
  }
  return ret;
}

vector<vector<ll>> powMatrix(vector<vector<ll>> a,ll x){
  int n=a.size();
  vector<vector<ll>> ret(n,vector<ll>(n,0));
  for(int i=0;i<n;i++){
    ret[i][i]=1;
  }
  while(x>0){
    if(x%2==1){
      ret=multi(a,ret);
    }
    a=multi(a,a);
    x>>=1;
  }
  return ret;
}

int main(){
  ll n;
  cin>>n;
  //偶数
  if(n%2==0){
    vector<vector<ll>> a(2,vector<ll>(2,1));
    a[1][1]=0;
    vector<vector<ll>> b(2,vector<ll>(1,1));
    //cout<<multi(a,b)[0][0]<<endl;
    cout<<multi(powMatrix(a,n/2),b)[1][0]<<'\n';
  }
  //奇数
  else{
    vector<vector<ll>> a(4,vector<ll>(4,0));
    a[0][0]=1;
    a[0][1]=1;
    a[1][0]=1;
    a[0][2]=2;
    a[2][2]=1;
    a[2][3]=1;
    a[3][2]=1;
    vector<vector<ll>> b(4,vector<ll>(1,0));
    b[0][0]=0;
    b[1][0]=0;
    b[2][0]=1;
    b[3][0]=0;
    auto ret=multi(powMatrix(a,(n+1)/2),b);
    ll ans=(ret[1][0]+ret[3][0])%MOD;
    cout<<ans<<'\n';

  }
}
0