题目链接

luoguP4000 斐波那契数列

题解

根据这个东西

https://www.cnblogs.com/sssy/p/9418732.html

我们可以找出%p意义下的循环节

然后就可以做了

人傻,自带,大,常数

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long
const LL maxn = 1000007;
LL dp[maxn * 10];
LL prime[maxn],s = 0;
bool vis[maxn];
void init_prime() {
for(LL i = 2;i < maxn;i ++) {
if(!vis[i]) prime[s ++] = i;
for(LL j = 0;j < s && i * prime[j] < maxn;j ++) {
vis[i * prime[j]] = 1;
if(i%prime[j] == 0) break;
}
}
}
LL pow_mod(LL a1,LL b1){
LL ret = 1;
for(;b1;b1 >>= 1,a1 *= a1)
if(b1 & 1) ret = ret * a1;
return ret;
}
LL pow_mod2(LL a,LL b,LL p) {
LL ret = 1;
for(;b;b >>= 1,a = a * a % p)
if(b & 1) ret = ret * a % p;
return ret;
}
LL gcd(LL a,LL b) { return b ? gcd(b,a % b) : a; }
bool f(LL n,LL p) {
if(pow_mod2(n,(p - 1) >> 1,p) != 1) return false;
return true;
}
struct matrix{
LL x1,x2,x3,x4;
};
matrix matrix_a,matrix_b;
matrix M2(matrix aa,matrix bb,LL mod){
matrix tmp;
tmp.x1 = (aa.x1 * bb.x1 % mod + aa.x2 * bb.x3 % mod) % mod;
tmp.x2 = (aa.x1 * bb.x2 % mod + aa.x2 * bb.x4 % mod) % mod;
tmp.x3 = (aa.x3 * bb.x1 % mod + aa.x4 * bb.x3 % mod) % mod;
tmp.x4 = (aa.x3 * bb.x2 % mod + aa.x4 * bb.x4 % mod) % mod;
return tmp;
}
matrix M(LL n,LL mod){
matrix a,b;
a=matrix_a;b=matrix_b;
for(;n;n >>= 1,a = M2(a,a,mod))
if(n & 1) b = M2(b,a,mod);
return b;
}
LL fac[100][2],l,x,fs[1000];
void dfs(LL count,LL step) {
if(step == l) {
fs[x ++] = count;
return ;
}
LL sum = 1;
for(LL i = 0;i < fac[step][1];++ i) {
sum *= fac[step][0];
dfs(count * sum,step + 1);
}
dfs(count,step + 1);
}
LL solve2(LL p){
if(p < 1e6 && dp[p]) return dp[p];
bool ok = f(5,p);//判断5是否为p的二次剩余
LL t;
if(ok) t = p - 1;
else t = 2 * p + 2;
l = 0;
for(LL i = 0;i < s;i ++){
if(prime[i] > t / prime[i]) break;
if(t % prime[i] == 0) {
LL count = 0;
fac[l][0] = prime[i];
while(t % prime[i] == 0) count ++ , t /= prime[i];
fac[l ++][1] = count;
}
}
if(t > 1) fac[l][0]=t, fac[l++][1]=1;
x = 0;
dfs(1 , 0); //求t的因子
sort(fs,fs + x);
for(LL i = 0;i < x;i ++) {
matrix m1 = M(fs[i],p);
if(m1.x1 == m1.x4 && m1.x1 == 1 && m1.x2 == m1.x3 && m1.x2 == 0) {
if(p < 1e6) dp[p] = fs[i];
return fs[i];
}
}
}
LL get_M(LL n){
LL ans = 1,cnt;
for(LL i = 0;i < s;i ++) {
if(prime[i] > n / prime[i]) break;
if(n % prime[i] == 0) {
LL count = 0;
while(n % prime[i] == 0) count ++,n /= prime[i];
cnt = pow_mod(prime[i],count - 1);
cnt *= solve2(prime[i]);
ans = (ans / gcd(ans , cnt)) * cnt;
}
}
if(n > 1){
cnt = 1;
cnt *= solve2(n);
ans = ans / gcd(ans,cnt) * cnt;
}
return ans;
}
char Ss[30000007];
struct bign {
int z[30000007],l;
void init() {
memset(z,0,sizeof(z));
scanf("%s",Ss + 1);
l = strlen(Ss + 1);
for(int i = 1;i <= l;i ++)
z[i] = Ss[l - i + 1] - '0';
}
LL operator % (const long long & a) const {
LL b = 0;
for (int i = l;i >= 1;i --)
b = (b * 10 + z[i]) % a;
return b;
}
}z;
LL m1;
struct Matrix {
LL a[3][3];
Matrix () { memset(a,0,sizeof a); }
Matrix operator * (const Matrix & p) const {
Matrix ret;
for(int i = 0;i <= 1;++ i)
for(int j = 0;j <= 1;++ j)
for(int k = 0;k <= 1;++ k)
ret.a[i][j] = (ret.a[i][j] + ( a[i][k] * p.a[k][j] ) % m1 ) % m1;
return ret;
}
};
LL solve(LL x) {
Matrix p,q;
p.a[0][0] = 1; p.a[0][1] = 1; p.a[1][0] = 1;
q.a[0][1] = 1;
for(;x;x >>= 1,p = p * p)
if(x & 1) q = q * p;
return q.a[0][0];
}
main() {
LL t,n;
init_prime();
matrix_a.x1 = matrix_a.x2 = matrix_a.x3 = 1;
matrix_a.x4 = 0;
matrix_b.x1 = matrix_b.x4 = 1;
matrix_b.x2 = matrix_b.x3 = 0;
dp[2] = 3;dp[3] = 8;dp[5] = 20;
z.init(); scanf("%lld",&n); m1 = n;
n = get_M(n);
//printf("%lld\n",n % m1);
n = z % n;
printf("%lld\n",solve(n));
return 0;
}

luoguP4000 斐波那契数列的更多相关文章

  1. C#求斐波那契数列第30项的值(递归和非递归)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 斐波拉契数列加强版——时间复杂度O(1),空间复杂度O(1)

    对于斐波拉契经典问题,我们都非常熟悉,通过递推公式F(n) = F(n - ) + F(n - ),我们可以在线性时间内求出第n项F(n),现在考虑斐波拉契的加强版,我们要求的项数n的范围为int范围 ...

  3. js中的斐波那契数列法

    //斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...

  4. 剑指Offer面试题:8.斐波那契数列

    一.题目:斐波那契数列 题目:写一个函数,输入n,求斐波那契(Fibonacci)数列的第n项.斐波那契数列的定义如下: 二.效率很低的解法 很多C/C++/C#/Java语言教科书在讲述递归函数的时 ...

  5. 算法: 斐波那契数列C/C++实现

    斐波那契数列: 1,1,2,3,5,8,13,21,34,....     //求斐波那契数列第n项的值 //1,1,2,3,5,8,13,21,34... //1.递归: //缺点:当n过大时,递归 ...

  6. 洛谷P1962 斐波那契数列 || P1349 广义斐波那契数列[矩阵乘法]

    P1962 斐波那契数列 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数 ...

  7. Python递归及斐波那契数列

    递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可 ...

  8. 简单Java算法程序实现!斐波那契数列函数~

    java编程基础--斐波那契数列 问题描述:一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 思路:可能出现的情况:(1) n=1 ,一种方法 ;(2)n=2 ...

  9. js 斐波那契数列(兔子问题)

    对于JS初学者来说,斐波那契数列一直是个头疼的问题,总是理不清思路. 希望看完这篇文章之后会对你有帮助. 什么是斐波那契数列 : 答: 斐波那契数列,又称黄金分割数列.因数学家列昂纳多·斐波那契(Le ...

随机推荐

  1. 基于docker的spark-hadoop分布式集群之二: 环境测试

    在上一章<环境搭建>基础上,本章对各个模块做个测试 Mysql 测试 1.Mysql节点准备 为方便测试,在mysql节点中,增加点数据 进入主节点 docker exec -it had ...

  2. IOS的属性和实例变量

    实际上,@property声明的是属性,并不是实例变量.但是编译器会根据属性,自动生成实例变量,和对应的access方法.所以已经在interface里声明了@property,就不再需要在imple ...

  3. 带你吃透RTMP

    RTMP协议是Real Time Message Protocol(实时信息传输协议)的缩写,它是由Adobe公司提出的一种应用层的协议,用来解决多媒体数据传输流的多路复用(Multiplexing) ...

  4. hdu 5385 The path

    http://acm.hdu.edu.cn/showproblem.php?pid=5385 题意: 给定一张n个点m条有向边的图,构造每条边的边权(边权为正整数),令d(x)表示1到x的最短路,使得 ...

  5. 流媒体技术学习笔记之(三)Nginx-Rtmp-Module统计某频道在线观看流的客户数

    获得订阅者人数,可以方便地显示观看流的客户数. 查看已经安装好的模块 /usr/local/nginx/sbin/nginx -V 安装从源编译Nginx和Nginx-RTMP所需的工具 sudo a ...

  6. 使用 git 托管代码

    1. 下载安装好 git 客户端 2. 找一个家代码托管平台 我用 coding.net,注册个账号,建一个空项目 然后打开安装好的 git bash 客户端,使用 git clone 命令克隆下远程 ...

  7. 20155306 2016-2017-2 《Java程序设计》第5周学习总结

    20155306 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承架构 Java中所有错误都会被打包为对象,运用try.c ...

  8. 【干货】使用EnCase来分析windows 7文件系统------认识元数据记录$MFT,数据恢复

    来源:Unit 6: Windows File Systems and Registry 6.1 Windows File Systems and Registry Windows NTFS File ...

  9. C#并行计算 Parallel.Foreach&Parallel.For

    Parallel.For(int fromInclude, int toExclude, Action<int> body) 栗子: Parallel.For(0, 10, (i) =&g ...

  10. docker之安装和管理mongodb

    前言 折腾一些使用docker来配置和管理mongodb和mongodb集群. 安装mongodb 从docker网站拉取mongodb镜像 docker search mongo # 选择一个版本 ...