結果
問題 | No.8036 Restricted Lucas (Easy) |
ユーザー |
![]() |
提出日時 | 2018-04-01 23:58:40 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 5 ms / 2,000 ms |
コード長 | 2,566 bytes |
コンパイル時間 | 733 ms |
コンパイル使用メモリ | 67,152 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-26 06:17:00 |
合計ジャッジ時間 | 1,255 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 6 |
ソースコード
#include<iostream>#include <vector>#include <stdlib.h>using namespace std;#define rep(i,a,b) for(int i=a;i<b;i++)typedef long long ll;int one;int two;int zero;ll N;ll mult(ll a, ll b){ll ans = zero;ll cur = a;while(b){if(b&one){ans += cur;}b >>= one;cur <<= one;}return ans;}struct Fibonacci {long long modf_(long long a){lldiv_t ans = lldiv(a, mod);return ans.rem;}ll mod;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) {return modf_(mult(x, y));}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 = one; while (b > zero) {if (b & one) ret = modf_(mult(ret, a)); a = modf_(mult(a, a)); b >>= one;} return ret;}int modinv(int a) { return modpow(a, mod - two); }typedef vector<ll> Vec;typedef vector<Vec> Mat;Vec mulMatVec(Mat a, Vec b){int n = b.size(); Vec ret(n, zero);rep(i, zero, n) rep(j, zero, 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, zero));rep(i, zero, n) rep(j, zero, n) rep(k, zero, 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(), zero));rep(i, zero, x.size()) ret[i][i] = one;while (zero < n) { if (!(n & one)) { x = mulMatMat(x, x); n >>= one; } else { ret = mulMatMat(ret, x); --n; } }return ret;}ll query(ll N, ll _mod) {mod = _mod;Mat m = Mat(two, Vec(two, zero));m[zero][zero] = one;m[zero][one] = one;m[one][zero] = one;Vec v = Vec(two, zero);v[zero] = one;v[one] = two;m = fastpow(m, N);v = mulMatVec(m, v);return v[one];}};int main() {one++;two++; two++;int seven = two + two + two + one;int ten = seven + two + one;ll mod = mult(ten,ten);mod = mult(mod, mod); mod = mult(mod, mod);mod = mult(mod, ten);mod += seven;int T; cin >> T;while(T--) {cin >> N;Fibonacci fib;ll ans = fib.query(N, mod);cout << ans << endl;}}