結果

問題 No.534 フィボナッチフィボナッチ数
ユーザー はまやんはまやん
提出日時 2017-06-24 00:08:17
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,838 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 188,740 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-03 03:56:26
合計ジャッジ時間 3,205 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
typedef long long ll;
struct A {
int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) {
int ret = 1; while (b > 0) {
if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1;
} return ret;
}
int modinv(int a) { return modpow(a, mod - 2); }
typedef vector<int> Vec;
typedef vector<Vec> Mat;
Vec mulMatVec(Mat a, Vec b)
{
int n = b.size(); Vec ret(n, 0);
rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
return ret;
}
Mat mulMatMat(Mat a, Mat b)
{
int n = a.size(); Mat ret(n, Vec(n, 0));
rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
return ret;
}
Mat fastpow(Mat x, ll n)
{
Mat ret(x.size(), Vec(x.size(), 0));
rep(i, 0, x.size()) ret[i][i] = 1;
while (0 < n) { if ((n % 2) == 0) { x = mulMatMat(x, x); n >>= 1; } else { ret = mulMatMat(ret, x); --n; } }
return ret;
}
void printVec(Vec a) { cout << "[\t"; rep(i, 0, a.size()) cout << a[i] << "\t"; cout << "]" << endl; }
void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); }
ll query(ll N) {
Mat m = Mat(2, Vec(2, 0));
m[0][0] = 1;
m[0][1] = 1;
m[1][0] = 1;
Vec v = Vec(2, 0);
v[0] = 1;
m = fastpow(m, N);
v = mulMatVec(m, v);
return v[1];
}
};
struct B {
ll mod = 2000000016LL;
ll add(ll x, ll y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> ll add(ll x, T... y) { return add(x, add(y...)); }
ll mul(ll x, ll y) {
string yy = to_string(y);
ll xx = x;
ll res = 0, d = 1;
for (char c : yy) {
ll cc = c - '0';
res *= 10;
res %= mod;
res += cc * x;
res %= mod;
}
return res;
}
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) {
int ret = 1; while (b > 0) {
if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1;
} return ret;
}
int modinv(int a) { return modpow(a, mod - 2); }
typedef vector<ll> Vec;
typedef vector<Vec> Mat;
Vec mulMatVec(Mat a, Vec b)
{
int n = b.size(); Vec ret(n, 0);
rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
return ret;
}
Mat mulMatMat(Mat a, Mat b)
{
int n = a.size(); Mat ret(n, Vec(n, 0));
rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
return ret;
}
Mat fastpow(Mat x, ll n)
{
Mat ret(x.size(), Vec(x.size(), 0));
rep(i, 0, x.size()) ret[i][i] = 1;
while (0 < n) { if ((n % 2) == 0) { x = mulMatMat(x, x); n >>= 1; } else { ret = mulMatMat(ret, x); --n; } }
return ret;
}
void printVec(Vec a) { cout << "[\t"; rep(i, 0, a.size()) cout << a[i] << "\t"; cout << "]" << endl; }
void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); }
ll query(ll N) {
Mat m = Mat(2, Vec(2, 0));
m[0][0] = 1;
m[0][1] = 1;
m[1][0] = 1;
Vec v = Vec(2, 0);
v[0] = 1;
m = fastpow(m, N);
v = mulMatVec(m, v);
return v[1];
}
};
/*---------------------------------------------------------------------------------------------------
           _
     _ ´<_   Welcome to My Coding Space!
     ´_` /  ⌒i
           | |
    /   //  |
  __(__ニ/  _/ .| .|____
     /____/ u 
---------------------------------------------------------------------------------------------------*/
ll N;
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N;
A a; B b;
ll n = b.query(N);
ll ans = a.query(n);
cout << ans << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0