R - M斐波那契数列

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description: 
System Crawler  (2016-04-24)

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, b, n( 0 <= a, b, n <= 10^9 )
 

Output

对每组测试数据请输出一个整数F[n],由于F[n]可能很大,你只需输出F[n]对1000000007取模后的值即可,每组数据输出一行。
 

Sample Input

0 1 0
6 10 2
 

Sample Output

0
60
 
 
列出数列的前几项可以观察得到,每一项的a,b的值满足f[i] = f[i-1] + f[i-2],所以能够用矩阵构造。 但是我们的答案最后要取mod,矩阵里的值可能也会超过long long,
所以也需要降幂,此时可以用欧拉降幂,如果有公式a^x (mod m),并且gcd(x,m)==1,即x和m互质,那么a^x = a^(x % Eular(m));由于m是质数,所以Eular(m) = m - 1;
最后再快速幂、相乘(要取模)即可;
 
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define MOD 1000000007
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct Mat
{
ll a[][];
};
ll fa,fb,n;
Mat operator *(Mat a,Mat b)
{
Mat c;
memset(c.a,,sizeof(c.a));
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
for(int k = ; k < ;k++){
c.a[i][j] += (a.a[i][k] * b.a[k][j])%(MOD-);
}
}
}
return c;
}
Mat power(Mat b,ll n)
{
Mat c;
c.a[][] = c.a[][] = ;
c.a[][] = c.a[][] = ;
while(n){
if(n & ){
c = c * b;
}
b = b * b;
n >>= ;
}
return c;
}
ll mod_pow(ll x,ll n)
{
ll res = ;
while(n){
if(n & ) res = res * x % MOD;
x = x * x % MOD;
n >>= ;
}
return res;
}
ll mod_mul(ll a,ll b)
{
ll res = ;
while(b){
if(b & ){
res = (res + a) % MOD;
}
b >>= ;
a = (a + a) % MOD;
}
return res;
}
int main()
{
while(~scanf("%lld%lld%lld",&fa,&fb,&n)){
if(fa == || fb == ){
cout<<<<endl;
continue;
}
if(n == ){
cout<<fa%MOD<<endl;
continue;
}
else if(n == ){
cout<<fb%MOD<<endl;
continue;
}
Mat a;
a.a[][] = ;
a.a[][] = a.a[][] = a.a[][] = ;
a = power(a,n-);
ll num1 = (a.a[][] + a.a[][])%(MOD-);
ll num2 = (a.a[][] + a.a[][])%(MOD-);
ll ans1 = mod_pow(fa,num1);
ll ans2 = mod_pow(fb,num2);
ll ans = mod_mul(ans1,ans2);
printf("%lld\n",ans);
}
return ;
}

hdu4549 矩阵快速幂 + 欧拉降幂的更多相关文章

  1. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...

  2. HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

    装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...

  3. HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理

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

  4. [bzoj 1409] Password 矩阵快速幂+欧拉函数

    考试的时候想到了矩阵快速幂+快速幂,但是忘(bu)了(hui)欧拉定理. 然后gg了35分. 题目显而易见,让求一个数的幂,幂是斐波那契数列里的一项,考虑到斐波那契也很大,所以我们就需要欧拉定理了 p ...

  5. hdu 5895(矩阵快速幂+欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895 f(n)=f(n-2)+2*f(n-1) f(n)*f(n-1)=f(n-2)*f(n-1)+2 ...

  6. HDU 4549 矩阵快速幂+快速幂+欧拉函数

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

  7. hdu4549矩阵快速幂+费马小定理

    转移矩阵很容易求就是|0  1|,第一项是|0| |1  1|             |1| 然后直接矩阵快速幂,要用到费马小定理 :假如p是质数,且gcd(a,p)=1,那么 a(p-1)≡1(m ...

  8. Super A^B mod C (快速幂+欧拉函数+欧拉定理)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...

  9. Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂

    https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...

随机推荐

  1. Codeforces 549G Happy Line[问题转换 sort]

    G. Happy Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. 第五章项目:QuickHit

    需求概述: 根据输入速率和正确率将玩家分为不同级别,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高.如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级(为了简单起见, ...

  3. 关于using关键字

    使用C#访问数据库资源需要如下几步: SqlConnection con=new SqlConnection(str); try { con.Open(); //略 } catch(Exception ...

  4. HTML插入地图的方法

    方法/步骤 1.打开"百度地图生成器"的网址:http://api.map.baidu.com/lbsapi/creatmap/index.html 如下图: 2.在"1 ...

  5. EntityFramework Core 封装

    public class EntityBaseRepository<T> : IEntityBaseRepository<T> where T : class, IEntity ...

  6. 花生壳动态IP域名解析之python自动提交公网IP

    #!/usr/bin/env python import re import os import time import random ip_current = '' while True: myip ...

  7. usb驱动开发22之驱动生命线

    我们总是很喜欢高潮,不是吗?那就好好对待她哦.我们来看一下linux中的高潮部分设备是怎么从Address进入Configured的. usb_set_configuration函数的代码就不贴了,可 ...

  8. 跟我学习Storm_Storm基本概念

    首先我们通过一个Storm和Hadoop的对比来了解Storm中的基本概念. 接下来我们再来具体看一下这些概念. Nimbus:负责资源分配和任务调度. Supervisor:负责接受nimbus分配 ...

  9. 实验一 Java开发环境的熟悉

    实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没 ...

  10. PRML读书会第十二章 Continuous Latent Variables(PCA,Principal Component Analysis,PPCA,核PCA,Autoencoder,非线性流形)

    主讲人 戴玮 (新浪微博: @戴玮_CASIA) Wilbur_中博(1954123) 20:00:49 我今天讲PRML的第十二章,连续隐变量.既然有连续隐变量,一定也有离散隐变量,那么离散隐变量是 ...