http://acm.hdu.edu.cn/showproblem.php?pid=4549

f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p.....f[n] = a^fib[n-1] * b^fib[n-2]%p。



这里p是质数,且a,p互素,那么我们求a^b%p,当b非常大时要对b降幂。

由于a,p互素,那么由费马小定理知a^(p-1)%p = 1。令b = k*(p-1) + b'。a^b%p = a^(k*(p-1)+b')%p = a^(k*(p-1))%p * a^b'%p

= a^b'%p。

当中p' = b%(p-1)。

那么上题的fib[n-1]和fib[n-2]能够经过矩阵高速幂对(p-1)取模,实现降幂以后再用高速幂求得。

事实上a^b%c较一般的形式是a^(b%phi[c]+phi[c])%c (b>=phi[c]),在c是素数或a与c互素的时候能够简化为a^(b%(c-1))%c。



纳闷了一中午,结果把fib数列弄错了。f[0] = 0, f[1] = 1,f[n] = f[n-1]+f[n-2](n >= 2)。第0项是0,sad....

http://www.narutoacm.com/archives/a-pow-b-mod-m/

对大神表示深深的ym.

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
#define C 240
#define S 20
using namespace std;
const int maxn = 110;
const int mod = 1000000007; struct matrix
{
LL mat[3][3];
void init()
{
memset(mat,0,sizeof(mat));
for(int i = 0; i < 2; i++)
mat[i][i] = 1;
}
}m; LL a,b,n; matrix mul_matrix(matrix x, matrix y)
{
matrix ans;
memset(ans.mat,0,sizeof(ans.mat));
for(int i = 0; i < 2; i++)
{
for(int k = 0; k < 2; k++)
{
if(x.mat[i][k] == 0) continue;
for(int j = 0; j < 2; j++)
ans.mat[i][j] = (ans.mat[i][j] + x.mat[i][k]*y.mat[k][j])%(mod-1);
}
}
return ans;
} matrix pow_matrix(matrix m, LL n)
{
matrix ans;
ans.init();
while(n)
{
if(n&1)
ans = mul_matrix(ans,m);
m = mul_matrix(m,m);
n >>= 1;
}
return ans;
} LL pow_mod(LL a, LL b)
{
LL res = 1;
a = a%mod;
while(b)
{
if(b&1)
res = res*a%mod;
a = a*a%mod;
b >>= 1;
}
return res;
} int main()
{ while(~scanf("%lld %lld %lld",&a,&b,&n))
{
m.mat[0][0] = m.mat[0][1] = m.mat[1][0] = 1;
m.mat[1][1] = 0;
if(n == 0)
{
printf("%lld\n",a%mod);
continue;
}
if(n == 1)
{
printf("%lld\n",b%mod);
continue;
}
else if(n == 2)
{
printf("%lld\n",a*b%mod);
continue;
}
matrix ans = pow_matrix(m,n);
LL res = pow_mod(a,ans.mat[1][1]) * pow_mod(b,ans.mat[0][1]) %mod;
printf("%lld\n",res);
}
return 0;
}

hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)的更多相关文章

  1. hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

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

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  3. [HDU 4549] M斐波那契数列

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  4. HDU 4549 M斐波那契数列(矩阵快速幂)

    题目链接:M斐波那契数列 题意:$F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]$.给定$a,b,n$,求$F[n]$. 题解:暴力打表后发现$ F[n]=a^{fib(n-1)} ...

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

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

  6. hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)

    Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...

  7. HDU 4549 M斐波那契数列(矩阵幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549 题意:F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]. 思路:手算一下可以发现 ...

  8. 斐波那契数列 矩阵乘法优化DP

    斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007​\),\(n\le 10^{18}​\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...

  9. HDU 1316 (斐波那契数列,大数相加,大数比较大小)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1316 Recall the definition of the Fibonacci numbers: ...

随机推荐

  1. 生成100个Div

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  2. Java Servlet DAO实践(二)

    Java Servlet DAO实践(二) DAO连接类 package com.seller.servlets.dao; import java.sql.*; public class DataBa ...

  3. datagrid总条数

    1.getData var data=$("#dg").datagrid("getData");alert('总数据量:' + data.total)//注意你 ...

  4. BZOJ 2223: [Coci 2009]PATULJCI 主席树

    Code: #include<bits/stdc++.h> #define maxn 300001 #define mid ((l+r)>>1) using namespace ...

  5. Spring自定义拦截器

    HandlerInterceptorAdapter由Spring MVC提供,用来拦截请求. 实现自定义拦截器需要继承HandlerInterceptorAdapter或实现HandlerInterc ...

  6. gnulpot

    gnulpot Table of Contents 1. Label position 2. coordinates 3. Symbols 4. key 4.1. key position 4.2. ...

  7. RAII手法封装互斥锁

    RAII手法是 Resource Acquisition is Initialization 的缩写,意为“资源获取即初始化”,在使用智能指针时也使用,下面是针对互斥量时的实现, #include & ...

  8. Hello Shiro

    [HelloWorld Shiro] 1.搭建开发环境-加入jar包 2.步骤(前提:已下载好Shiro资源包): ①找到shiro-root-1.2.3-source-release包, ②按Apa ...

  9. Spring MVC学习总结(11)——Spring MVC集成Swagger跨域问题

      <!-- CORS配置,为了让别的机器访问本机的swagger接口文档服务 -->          <dependency>              <group ...

  10. C#装饰模式

    using System;using System.Collections.Generic;using System.Text; namespace 装饰模式{    class Person    ...