题目传送门

https://lydsy.com/JudgeOnline/problem.php?id=5118

题解

这个题一看就是不可做的样子。

求斐波那契数列的第 \(n\) 项,\(n \leq 2^{10^{15}}\)???

这样人怎么矩阵快速幂啊。


等等这个模数很神奇啊。

\(1125899839733759\) 好像是一个质数,还以 \(9\) 结尾。

那么 \(5\) 对于 \(1125899839733759\) 一定有二次剩余咯。

那么根据 Fib 的通项公式

\[f(n) = (\frac{\sqrt 5+1}{2})^n + (\frac{\sqrt 5 - 1}2)^n
\]

那么这个 \(2^n\) 就可以根据费马小定理就可以转化成 \(n \bmod P-1\) 了。

那么这个 \(2^n\) 的范围瞬间小了很多。

于是就可以直接矩阵快速幂了,注意乘法要用快速乘实现。


UPD: 发现好像傻掉了,都有通项公式了,还写什么矩阵快速幂。


时间复杂度 \(O(T\log^2 n)\)。

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const ll P = 1125899839733759; ll n; inline void sadd(ll &x, ll y, const ll &P = ::P) { x += y; x >= P ? x -= P : x; }
inline ll smod(const ll &x, const ll &P = ::P) { return x >= P ? x - P : x; }
inline ll fmul(ll x, ll y, const ll &P = ::P) {
ll ans = 0;
for (; y; y >>= 1, sadd(x, x, P)) if (y & 1) sadd(ans, x, P);
return ans;
}
inline ll fpow(ll x, ll y, const ll &P = ::P) {
ll ans = 1;
for (; y; y >>= 1, x = fmul(x, x, P)) if (y & 1) ans = fmul(ans, x, P);
return ans;
} struct Matrix {
ll a[2][2]; inline Matrix() { memset(a, 0, sizeof(a)); }
inline Matrix(const ull &x) {
memset(a, 0, sizeof(a));
a[0][0] = a[1][1] = x;
} inline Matrix operator * (const Matrix &b) {
Matrix c;
c.a[0][0] = smod(fmul(a[0][0], b.a[0][0]) + fmul(a[0][1], b.a[1][0]));
c.a[0][1] = smod(fmul(a[0][0], b.a[0][1]) + fmul(a[0][1], b.a[1][1]));
c.a[1][0] = smod(fmul(a[1][0], b.a[0][0]) + fmul(a[1][1], b.a[1][0]));
c.a[1][1] = smod(fmul(a[1][0], b.a[0][1]) + fmul(a[1][1], b.a[1][1]));
return c;
}
} A, B; inline Matrix fpow(Matrix x, ll y) {
Matrix ans(1);
for (; y; y >>= 1, x = x * x) if (y & 1) ans = ans * x;
return ans;
} inline void work() {
A.a[0][0] = 1, A.a[0][1] = 1;
A.a[1][0] = 1, A.a[1][1] = 0;
B.a[0][0] = 0, B.a[1][0] = 1;
A = fpow(A, n) * B;
printf("%lld\n", A.a[0][0]);
} inline void init() {
read(n);
n = fpow(2, n, P - 1);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
int T;
read(T);
while (T--) {
init();
work();
}
fclose(stdin), fclose(stdout);
return 0;
}

bzoj5118 Fib数列2 二次剩余+矩阵快速幂的更多相关文章

  1. BZOJ5118 Fib数列2(矩阵快速幂)

    特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...

  2. nyoj_148_fibonacci数列(二)_矩阵快速幂

    fibonacci数列(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 In the Fibonacci integer sequence, F0 = 0, F ...

  3. fibonacci数列(二)_矩阵快速幂

    描述 In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For exampl ...

  4. BZOJ5118: Fib数列2(二次剩余)

    题意 题目链接 题目链接 一种做法是直接用欧拉降幂算出\(2^p \pmod{p - 1}\)然后矩阵快速幂. 但是今天学习了一下二次剩余,也可以用通项公式+二次剩余做. 就是我们猜想\(5\)在这个 ...

  5. POJ3070 斐波那契数列递推 矩阵快速幂模板题

    题目分析: 对于给出的n,求出斐波那契数列第n项的最后4为数,当n很大的时候,普通的递推会超时,这里介绍用矩阵快速幂解决当递推次数很大时的结果,这里矩阵已经给出,直接计算即可 #include< ...

  6. Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)

    题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...

  7. BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂

    http://www.lydsy.com/JudgeOnline/problem.php?id=3231   和斐波那契一个道理在最后加一个求和即可 #include<cstdio> #i ...

  8. HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)

    M斐波那契数列 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submi ...

  9. hihocoder第42周 3*N骨牌覆盖(状态dp+矩阵快速幂)

    http://hihocoder.com/contest/hiho42/problem/1 给定一个n,问我们3*n的矩阵有多少种覆盖的方法 第41周做的骨牌覆盖是2*n的,状态转移方程是dp[i] ...

随机推荐

  1. 大数据笔记(十四)——HBase的过滤器与Mapreduce

    一. HBase过滤器 1.列值过滤器 2.列名前缀过滤器 3.多个列名前缀过滤器 4.行键过滤器5.组合过滤器 package demo; import javax.swing.RowFilter; ...

  2. Qt 之 ZIP开源库 QuaZIP

    2013-10-31 21:46 10856人阅读 评论(0) 收藏 举报  分类: Qt(12)  版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   一.简介     ...

  3. Apache2.4+PHP7.2环境搭建

    Editplus生成码:http://www.jb51.net/tools/editplus/ 阿帕奇下载地址:https://www.apachehaus.com/cgi-bin/download. ...

  4. hive_action

    w pdf469 [不直接MR访问数据的工具   查询间接转化为MR] https://en.wikipedia.org/wiki/Apache_Hive Apache Hive supports a ...

  5. Eclipse Java工程转为Web工程步骤

    找到工程的.project文件,在<natures>标签中增加以下两行配置:<nature>org.eclipse.wst.common.modulecore.ModuleCo ...

  6. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第3节 Random类_11-练习二_猜数字小游

    0到100之间的数字.猜多少次才能猜对最终的结果.大了或者小了都会告诉你. 二分法查找. 循环次数不确定用whilte true的方式去循环 前两种情况是需要重试的 把猜测的代码放在whilte循环里 ...

  7. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_01 Collection集合_1_Collection集合概述

  8. spring boot添加logging不能启动且不报错

    1.问题: application.yml中添加logging启动失败,不报错,去除后又正常 logging: config: classpath:test-logback-spring.xml报错 ...

  9. Linux pip命令报错 -bash: pip: command not found

    下载 $ wget https://bootstrap.pypa.io/get-pip.py 安装 $ python get-pip.py 查看版本 $ pip -V 查看安装路径: find / - ...

  10. 【Unity Shader】---Alpha Blending的意义

    Alpha Blending 即Alpha混合 Blending 就是处理透明度的,处理光栅化最后阶段,显示在屏幕上的颜色 1 Blend Off 关闭alpha混合 2 混合公式:Blend Src ...