結果
| 問題 |
No.657 テトラナッチ数列 Easy
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-20 03:35:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 125 ms / 2,000 ms |
| コード長 | 2,826 bytes |
| コンパイル時間 | 2,182 ms |
| コンパイル使用メモリ | 202,744 KB |
| 最終ジャッジ日時 | 2025-01-15 11:51:37 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 |
ソースコード
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
// constexpr ll mod=1e9+7;
constexpr ll mod=17;
template<class T>
struct Mat{
vector<vector<T>> A;
Mat(){}
Mat(size_t n,size_t m):A(n,vector<T>(m,0)){}
Mat(size_t n):A(n,vector<T>(n,0)){};
size_t height() const{
return A.size();
}
size_t width() const{
return A[0].size();
}
inline const vector<T> &operator[](int k) const{
return A.at(k);
}
inline vector<T> &operator[](int k){
return A.at(k);
}
static Mat I(size_t n){
Mat mat(n);
for(int i=0;i<n;i++){
mat[i][i]=1;
}
return mat;
}
Mat &operator+=(const Mat &B){
size_t n=height(),m=width();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
(*this)[i][j]=((*this)[i][j]+B[i][j])%mod;
}
}
return (*this);
}
Mat &operator-=(const Mat &B){
size_t n=height(),m=width();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
(*this)[i][j]=((*this)[i][j]-B[i][j])%mod;
if((*this)[i][j]<0)(*this)[i][j]+=mod;
}
}
return (*this);
}
Mat &operator*=(const Mat &B){
int n=height(),m=B.width(),p=width();
assert(p==B.height());
vector<vector<T>> C(n,vector<T>(m,0));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int k=0;k<p;k++){
C[i][j]=(C[i][j]+(*this)[i][k]*B[k][j])%mod;
}
}
}
A.swap(C);
return (*this);
}
Mat &operator^=(ll k){
Mat B=Mat::I(height());
while(k){
if(k%2)B*=(*this);
(*this)*=(*this);
k>>=1LL;
}
A.swap(B.A);
return (*this);
}
Mat operator+(const Mat &B) const{
return (Mat(*this)+=B);
}
Mat operator-(const Mat &B) const{
return (Mat(*this)-=B);
}
Mat operator*(const Mat &B) const{
return (Mat(*this)*=B);
}
Mat operator^(const Mat &B) const{
return (Mat(*this)^=B);
}
};
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int q; cin >> q;
while(q--){
ll n; cin >> n;
if(n<4){
printf("0\n");
}
else if(n==4){
printf("1\n");
}
else{
Mat<ll> A(4,1),B(4);
A[0][0]=1;
B[0][0]=B[0][1]=B[0][2]=B[0][3]=B[1][0]=B[2][1]=B[3][2]=1;
B^=(n-4);
A=B*A;
printf("%lld\n",A[0][0]);
}
}
}