题目传送门

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. Microsoft windows terminal

    https://github.com/microsoft/terminal 尝试在windows store中安装,结果everything搜索不到 I tried running WindowsTe ...

  2. linux 杀掉僵尸进程 (zombie process, defunct)

    本文说明为什么会出现僵尸进程 (zombie process, defunct),以及如何杀掉僵尸进程 1. 为什么有僵尸进程 僵尸进程出现在父进程没有回收子进程的 PCB 的时候,这个时候子进程已经 ...

  3. leetcode-mid-Linked list-160 Intersection of Two Linked Lists-NO

    mycode 用了反转链表,所以不符合题意 参考: 思路: 1 先让长的链表先走,然后相同长度下看是否相遇 class Solution(object): def getIntersectionNod ...

  4. /etc/init.d# ./redis-server start

    root@ubuntu:/etc/init.d# ll total drwxr-xr-x root root May : ./ drwxr-xr-x root root May : ../ -rwxr ...

  5. flex embed 使用

    Flex 软件中经常需要使用一些外部的资源,如图片.声音.SWF或字体,虽然你也可以在软件运行的时候引入和载入,但是也可能经常需要直接将这些资源编译(Compile)到软件中,也就是直接嵌入资源(Em ...

  6. Openstack_通用技术_RPC 远程异步调用

    目录 目录 RPC 一个通过 HTTP Request 调用操作函数的 RPC 实现样例 环境 接收 HTTP Request RPC 调用具体的操作函数 测试 RPC RPC: 同一个项目内的不同服 ...

  7. beego 注解路由无效问题分析

    问题描述:学习 beego 框架发现注解路由无效,除了不能找到路由外,未见任何异常. 问题解决:将配置文件中的 runmode 更改为 dev 模式. 问题分析: 如果没有设置过 runmode 不会 ...

  8. 【MM系列】SAP MM模块-委外采购订单 把Warning转换成Error信息提示

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-委外采购订单 把W ...

  9. 20190901 On Java8 第十五章 异常

    第十五章 异常 要想创建健壮的系统,它的每一个构件都必须是健壮的. 异常概念 C++的异常处理机制基于 Ada,Java 中的异常处理则建立在 C++的基础之上(尽管看上去更像 Object Pasc ...

  10. IO流 -字符输入输出流,以及异常处理方法

    字符输入流 java.io.Reader: 字符输入流的顶层抽象父类 共性的成员方法: int read() 读取单个字符,并返回. int read(char[] cbuf) 将字符读入数组. ab ...