题目传送门

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. CentOS6.5/7安装配置Samba

    CentOS6.5安装配置Samba 本文的场景是虚拟机运行CentOS6.,本机是Win7,现欲把CentOS上的一个文件夹共享出来,Win的机器可以读写. Samba与window连接需要使用Ne ...

  2. Python分析《武林外传》 -----转载

    转载原链接:http://www.cnblogs.com/fredkeke/p/8328387.html 我一向比较喜欢看武侠电影.小说,但是06年武林外传开播的时候并没有追剧,简单扫几眼之后发现他们 ...

  3. C# 防火墙操作之特定程序

    将特定程序加入防火墙组,与将特定端口加入防火墙流程类似.详情见“C# 防火墙操作之特定端口”.其主要代码为: /// <summary> /// 允许应用程序通过防火墙 /// </ ...

  4. delphi中的inpubox,如何能控制它的位置? 10

    https://zhidao.baidu.com/question/153270855.html delphi中的inpubox,如何能控制它的位置? 10 RT ! 前辈!最好你就把那代码都拿出来吧 ...

  5. python3 configparser模块

    配置文件如下: import configparser conf = configparser.ConfigParser() print(type(conf)) #conf是类 conf.read(' ...

  6. Android Studio 连接夜神模拟器

    网上找到的解决是需要我们 然后运行cmd命令,cd到夜神安装目录,执行命令 nox_adb.exe connect 127.0.0.1:62001

  7. python绘制五角星

    问题描述: python中运用turtle图形模块绘制五角星 问题分析: turtle绘制图形时,得知图形中重要点的坐标非常重要. 于是,绘制五角星问题转化成为一个数学问题,计算五个顶点坐标即可. 已 ...

  8. 使用OkHttp模拟登陆LeetCode

    前言 网上有很多模拟登陆 LeetCode 的教程,但是基本都是使用 Python 来实现的.作为一个 Java 语言爱好者,因此想用 Java 来实现下.在实现的过程中,也遇到了一些坑点,故在此作为 ...

  9. .aspx和.aspx.cs之间的区别

    在vs里面创建一个web窗体会产生两种文件:后缀是.aspx和.aspx.cs. 简单的来说,.aspx是表现层,可以简单理解为是写html代码的,界面的设计部分:.cs是对应的逻辑代码,再通过特定的 ...

  10. Log4Net使用详解(续)

    转:http://blog.csdn.net/zhoufoxcn/article/details/6029021 说明自从上次在2008年在博客上发表过有关log4net的用法介绍文章之后(网址:ht ...