結果
問題 | No.344 ある無理数の累乗 |
ユーザー |
![]() |
提出日時 | 2016-02-13 00:33:02 |
言語 | C++11 (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,697 bytes |
コンパイル時間 | 938 ms |
コンパイル使用メモリ | 96,740 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-22 05:10:37 |
合計ジャッジ時間 | 1,846 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 30 |
ソースコード
#include <iostream>#include <vector>#include <cstdio>#include <sstream>#include <map>#include <string>#include <algorithm>#include <queue>#include <cmath>#include <functional>#include <set>#include <ctime>#include <random>using namespace std;template<typename T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}template<typename T> istream& operator , (istream& is, T& val){ return is >> val;}template<typename T> ostream& operator << (ostream& os, const vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"\n":"");return os;}template<typename T> ostream& operator , (ostream& os, T& val){ return os << " " << val;}template<typename T> ostream& operator >> (ostream& os, T& val){ return os << " " << val;}using namespace std;//[n*p] * [p*m] => [n*m]template<class T>vector< vector<T> > multmat(const vector<vector<T> > &A, const vector<vector<T>> &B, int n, int p, int m){vector<vector<T> > C(n, vector<T>(m,0));for(int i=0; i<n; i++){for(int k=0; k<p; k++){for(int j=0; j<m; j++){C[i][j] += A[i][k] * B[k][j];C[i][j] %= 1000;}}}return C;}//A[n*n]^ktemplate<class T>vector< vector<T> > mat_pow(vector<vector<T> > A, long long k){int n = A.size();vector<vector<T> > ret(n, vector<T>(n, 0) );for(int i=0; i<n; i++){ret[i][i] = 1;}while(k>0){if(k&1) ret = multmat(A,ret, n,n,n);A = multmat(A,A, n,n,n);k>>=1;}return ret;}int main(){long long n;cin >> n;vector<vector<long long>> mat = {{1,3}, {1,1}};auto ans = mat_pow(mat, n);long long ans_ = (ans[0][0] * 2 + 1000 + (n%2?0:-1) ) % 1000;cout << ans_ << endl;return 0;}