結果
問題 | No.658 テトラナッチ数列 Hard |
ユーザー | algon_320 |
提出日時 | 2018-03-02 23:09:24 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 557 ms / 2,000 ms |
コード長 | 3,545 bytes |
コンパイル時間 | 1,769 ms |
コンパイル使用メモリ | 180,604 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-22 06:07:42 |
合計ジャッジ時間 | 4,766 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 3 ms
6,940 KB |
testcase_04 | AC | 192 ms
6,940 KB |
testcase_05 | AC | 237 ms
6,940 KB |
testcase_06 | AC | 283 ms
6,944 KB |
testcase_07 | AC | 301 ms
6,944 KB |
testcase_08 | AC | 356 ms
6,940 KB |
testcase_09 | AC | 547 ms
6,944 KB |
testcase_10 | AC | 557 ms
6,944 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define int long long #define stoi stoll using ll=long long; using vi=vector<int>; using pii=pair<int,int>; #define ALL(c) begin(c),end(c) #define RALL(c) rbegin(c),rend(c) #define ITR(i,b,e) for(auto i=(b);i!=(e);++i) #define FORE(x,c) for(auto &x:c) #define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i) #define REP(i,n) REPF(i,0,n) #define REPR(i,n) for(int i=(int)(n);i>=0;--i) #define SZ(c) ((int)c.size()) #define CONTAIN(c,x) (c.find(x)!=end(c)) #define OUTOFRANGE(y,x,h,w) ((y)<0||(x)<0||(y)>=(h)||(x)>=(w)) #define dump(...) const signed INF_=1001001001; const ll INF=1001001001001001001LL; const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0}; template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;} template<class T> istream& operator>>(istream &is,vector<T> &v) { ITR(i,begin(v),end(v)) is>>*i;return is;} template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) { is>>p.first>>p.second;return is;} template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;} template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;} template<class T> using heap=priority_queue<T,vector<T>,greater<T>>; struct{template<class T> operator T(){T x;cin>>x;return x;}} IN; struct before_main_function { before_main_function() { cin.tie(0);ios::sync_with_stdio(false); cout<<setprecision(15)<<fixed; #define endl "\n" } } before_main_function; //------------------8<------------------------------------8<-------------------- using matrix = vector<vector<int>>; void resize_matrix(matrix &A, int h, int w, int fill) { A.resize(h); for (int i = 0; i < h; i++) { A[i].resize(w, fill); } } matrix multiple_matrix(matrix &A, matrix &B, function<int(int, int)> add = [](int a, int b){return a + b;}, function<int(int, int)> mul = [](int a, int b){return a * b;}) { int m = A.size(); int n = A[0].size(); assert(n == B.size()); int l = B[0].size(); matrix res; resize_matrix(res, m, l, 0); for (int i = 0; i < m; i++) { for (int j = 0; j < l; j++) { int tmp = 0; for (int k = 0; k < n; k++) { int p = mul(A[i][k], B[k][j]); tmp = add(tmp, p); } res[i][j] = tmp; } } return res; } matrix pow_matrix(matrix A, int k, function<int(int, int)> add = [](int a, int b){return a + b;}, function<int(int, int)> mul = [](int a, int b){return a * b;}) { int n = A.size(); assert(n == A[0].size()); matrix res; resize_matrix(res, n, n, 0); for (int i = 0; i < n; i++) res[i][i] = 1; while (k > 0) { if (k & 1) res = multiple_matrix(res, A, add, mul); A = multiple_matrix(A, A, add, mul); k >>= 1; } return res; } function<int(int, int)> add = [](int a, int b){return (a + b)%17;}; function<int(int, int)> mul = [](int a, int b){return (a * b)%17;}; int T(int n) { if(1<=n && n<=3) return 0; if(n==4) return 1; matrix t,s; resize_matrix(t,4,1,0); t[0][0]=1; resize_matrix(s,4,4,0); REP(i,4) s[0][i]=1; REP(i,3) s[i+1][i]=1; n-=4; auto u=pow_matrix(s,n,add,mul); auto res=multiple_matrix(u,t,add,mul); return res[0][0]%17; } signed main() { int Q; cin>>Q; while(Q--) { int n; cin>>n; cout<<T(n)<<endl; } return 0; }