bzoj5118: Fib数列2(费马小定理+矩阵快速幂)

  题目大意:求fib(2^n)

  就是求fib矩阵的(2^n)次方%p,p是质数,根据费马小定理有

  于是这题就完了

  注意因为模数比较大会爆LL,得写快速乘法...

#include<bits/stdc++.h>
#define ll long long
#define MOD(x) ((x)>=mod?(x-mod):(x))
using namespace std;
const int maxn=500010;
const ll mod=1125899839733759;
struct mtx{ll mp[2][2];mtx(){memset(mp, 0, sizeof(mp));}}ans, base;
ll n, T;
inline ll mul(ll a, ll b, ll mod)
{
    ll ans=0; a%=mod;
    for(;b;b>>=1, a=MOD(a+a))
    if(b&1) ans=MOD(ans+a);
    return ans;
}
mtx operator*(mtx a, mtx b)
{
    mtx c;
    for(int i=0;i<2;i++)
    for(int j=0;j<2;j++)
    for(int k=0;k<2;k++)
    c.mp[i][j]=MOD(c.mp[i][j]+mul(a.mp[i][k], b.mp[k][j], mod));
    return c;
}
inline ll power(ll a, ll b, ll mod)
{
    ll ans=1;
    for(;b;b>>=1, a=mul(a, a, mod))
    if(b&1) ans=mul(ans, a, mod);
    return ans;
}
inline ll mtx_power(ll b)
{
    if(!b) return 0;
    for(;b;b>>=1, base=base*base)
    if(b&1) ans=ans*base;
    return ans.mp[1][0];
}
int main()
{
    scanf("%lld", &T);
    while(T--)
    {
        scanf("%lld", &n);
        ll t=power(2, n, mod-1);
        base.mp[0][0]=base.mp[0][1]=base.mp[1][0]=1; base.mp[1][1]=0;
        ans.mp[0][0]=ans.mp[1][1]=1; ans.mp[1][0]=ans.mp[0][1]=0;
        printf("%lld\n", mtx_power(t));
    }
}

时间: 2024-08-27 15:37:58

bzoj5118: Fib数列2(费马小定理+矩阵快速幂)的相关文章

[bzoj5118]Fib数列2_费马小定理_矩阵乘法

Fib数列2 bzoj-5118 题目大意:求Fib($2^n$). 注释:$1\le n\le 10^{15}$. 想法:开始一看觉得一定是道神题,多好的题面啊?结果...妈的,模数是质数,费马小定理就tm完事了,将fib数列的通项公式列出来然后费马小定理... 最后,附上丑陋的代码... ...(照着郭爷一顿瞎jb敲) #include <iostream> #include <cstdio> #include <cstring> #include <algo

HDOJ 4549 M斐波那契数列 费马小定理+矩阵快速幂

MF( i ) = a ^ fib( i-1 ) * b ^ fib ( i )   ( i>=3) mod 1000000007 是质数 , 根据费马小定理  a^phi( p ) = 1 ( mod p )  这里 p 为质数 且 a 比 p小 所以 a^( p - 1 ) = 1 ( mod p ) 所以对很大的指数可以化简  a ^ k % p  == a ^ ( k %(p-1) ) % p 用矩阵快速幂求fib数后代入即可 M斐波那契数列 Time Limit: 3000/1000

HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)

M斐波那契数列 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的值吗? Input 输入包含多组测试数据: 每组数据占一行,包含3个整数a,

HDOJ 5667//费马小定理 矩阵快速幂

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意:如题给了一个函数式,给你a,b,c,n,p的值,叫你求f(n)%p的值 思路:先对函数取以a为底的log,令g(n)=log(a)(f(n)),结果就能得到 g(n)=b+c*g(n-1)+g(n-2);(n>3) g(n)=0;(n=1) g(n)=b;(n=2) g(n)  c  1  1  g(n-1) 用矩阵表示出来就是 g(n-1) = 1  0  0    *  g(n-2)  

【费马小定理+矩阵快速幂】HDU4549——M斐波那契数列

[题目大意] M斐波那契数列F[n]是一种整数数列,它的定义如下:F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 )现在给出a, b, n,求出F[n]的值. [思路] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 typedef long

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

M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1672    Accepted Submission(s): 482 Problem Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a,

hdu 4869 Turn the pokers(组合数+费马小定理)

Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. She went out and bought m pokers, tending to play poker. But she hated the traditional gameplay. She wants to change. She puts these pokers face down,

HDU4869:Turn the pokers(费马小定理+快速幂)

Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. She went out and bought m pokers, tending to play poker. But she hated the traditional gameplay. She wants to change. She puts these pokers face down,

HDU 4549 M斐波那契数列(矩阵快速幂&amp;费马小定理)

ps:今天和战友聊到矩阵快速幂,想到前几天学长推荐去刷矩阵专题,挑了其中唯一一道中文题,没想到越过山却被河挡住去路... 题目链接:[kuangbin带你飞]专题十九 矩阵 R - M斐波那契数列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u 题意 Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2]