結果
| 問題 |
No.720 行列のできるフィボナッチ数列道場 (2)
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2019-09-23 21:09:30 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
RE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 2,103 bytes |
| コンパイル時間 | 645 ms |
| コンパイル使用メモリ | 67,968 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-19 04:33:40 |
| 合計ジャッジ時間 | 4,187 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 3 |
| other | RE * 20 |
コンパイルメッセージ
main.cpp: In function ‘bool prod(matrix<T>, matrix<T>, matrix<T>&) [with T = long long int]’:
main.cpp:33:1: warning: control reaches end of non-void function [-Wreturn-type]
33 | }
| ^
ソースコード
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
template <typename T>
struct matrix{
int n, m;
vector<vector<T>> dat;
matrix(int n_, int m_){
n = n_; m = m_;
for(int i = 0; i < n; i++){
dat.push_back(vector<T>(m, 0));
}
}
};
template <typename T>
bool prod(matrix<T> a, matrix<T> b, matrix<T> &ans){
if(a.m != b.n) return false;
for(int i = 0; i < a.n; i++){
for(int j = 0; j < a.m; j++){
ans.dat[i][j] = 0;
for(int k = 0; k < b.m; k++){
ans.dat[i][j] += a.dat[i][k]*b.dat[k][j];
ans.dat[i][j] %= MOD;
}
}
}
}
template <typename T>
void copy_mat(matrix<T> a, matrix<T> &b){
for(int i = 0; i < a.n; i++){
for(int j = 0; j < a.m; j++){
b.dat[i][j] = a.dat[i][j];
}
}
}
template <typename T>
void pow(matrix<T> a, ll n, matrix<T> &ans){
matrix<T> buf(a.n, a.n);
matrix<T> tmp(a.n, a.n);
copy_mat(a, tmp);
for(int i = 0; i < a.n; i++) {
for(int j = 0; j < a.n; j++){
ans.dat[i][j] = 0;
}
ans.dat[i][i] = 1;
}
for(int i = 0; i <= 60; i++){
ll m = (ll)1 << i;
if(m&n){
prod(tmp, ans, buf);
copy_mat(buf, ans);
}
prod(tmp, tmp, buf);
copy_mat(buf, tmp);
}
}
template <typename T>
void print_mat(matrix<T> a){
for(int i = 0; i < a.n; i++){
for(int j = 0; j < a.m; j++){
cout << a.dat[i][j] << ' ';
}
cout << endl;
}
}
int main(){
ll N, M;
cin >> N >> M;
matrix<ll> a(2, 2);
a.dat[0][0] = 1;
a.dat[1][0] = 1;
a.dat[0][1] = 1;
a.dat[1][1] = 0;
matrix<ll> m(2, 2);
pow(a, M, m);
//print_mat(m);
matrix<ll> b(4, 4), ans(4, 4);
copy_mat(m, b);
for(int i = 0; i < 2; i++){
b.dat[i+2][i] = 1;
b.dat[i+2][i+2] = 1;
}
//print_mat(b);
pow(b, N+1, ans);
//print_mat(ans);
cout << ans.dat[3][0] << endl;
}
milanis48663220