結果

問題 No.344 ある無理数の累乗
ユーザー koyumeishikoyumeishi
提出日時 2016-02-13 00:33:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,697 bytes
コンパイル時間 1,849 ms
コンパイル使用メモリ 96,440 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 03:47:31
合計ジャッジ時間 2,905 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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]^k 
template<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;
}
0