M斐波那契数列

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3024    Accepted Submission(s): 930

Problem 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
 
题意:斐波那契数列变形
题解:很容易可以看出F[N] = b^(f(n))*a^(f(n-1))
f(n)为斐波那契数列第n项
接着求;b^(f(n))%mod == b^(t)%mod   (f(n)%y(mod) == t)
证:
根据欧拉函数求得y(mod)
 设 t = f(n)%y(mod)
  f(n) = k*y(mod)+t
欧拉定理:
  b^(y(mod))%mod == 1 (mod与b互质)
既b^(f(n))%mod == b^(k*y(mod)+t)%mod == b^(t)%mod
求t即可
t用矩阵快速幂可以求出
#include<bits/stdc++.h>
#define N 2
#define mes(x) memset(x, 0, sizeof(x));
#define ll __int64
long long mod = 1e9+;
const int MAX = 1e9+;
using namespace std;
struct mat{
ll a[N][N];
mat(){
memset(a, , sizeof(a));
}
mat operator *(mat b){
mat c;
for(int i=;i<N;i++)
for(int j=;j<N;j++)
for(int k=;k<N;k++)
c.a[i][j] = (c.a[i][j]+a[i][k]*b.a[k][j])%mod;
return c;
}
};
mat f(mat x,ll m){
mat t;
for(int i=;i<N;i++)
t.a[i][i] = ;
while(m){
if(m% == ) t = t*x;
x = x*x;
m >>= ;
}
return t;
}
ll powermod(ll a, ll b, ll c){
ll ans = ;
a = a % c;
while(b>) {
if(b % == )
ans = (ans * a) % c;
b = b/;
a = (a * a) % c;
}
return ans;
}
ll eular(ll n)
{
ll ret=,i;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
n/=i,ret*=i-;
while(n%i==) n/=i,ret*=i;
}
}
if(n>) ret*=n-;
return ret;
}
ll fmod(ll n){//第n项斐波那契取模
if(n==||n==-) return ;
if(n==) return ;
mat A, s;
s.a[][] = ;s.a[][] = ;
A.a[][] = A.a[][] = A.a[][] = ;
s = s*f(A, n-);
return s.a[][];
}
int main()
{
ll n, a, b, ans;
mod = eular(mod);
while(~scanf("%I64d%I64d%I64d", &a, &b, &n)){
//F(n) = a^(f(n-1))*b^(f(n));
ans = powermod(a, fmod(n-), MAX)*powermod(b, fmod(n), MAX)%MAX;
printf("%I64d\n", ans);
}
return ;
}
 

HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理的更多相关文章

  1. hdu4549 M斐波那契数列 矩阵快速幂+快速幂

    M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的 ...

  2. 51nod1242 斐波那契数列 矩阵快速幂

    1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 #include<stdio.h> #define mod 100000000 ...

  3. POJ3070 斐波那契数列 矩阵快速幂

    题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...

  4. 洛谷P1962 斐波那契数列(矩阵快速幂)

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

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

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

  6. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

  7. 洛谷- P1306 斐波那契公约数 - 矩阵快速幂 斐波那契性质

    P1306 斐波那契公约数:https://www.luogu.org/problemnew/show/P1306 这道题目就是求第n项和第m项的斐波那契数字,然后让这两个数求GCD,输出答案的后8位 ...

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

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

  9. poj3070 (斐波那契,矩阵快速幂)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9630   Accepted: 6839 Descrip ...

随机推荐

  1. jnlp jws

    http://www.mkyong.com/java/java-web-start-jnlp-tutorial-unofficial-guide/

  2. Android Notification通知栏使用

    package com.example.mynotifycation; import android.app.Activity; import android.app.Notification; im ...

  3. drds 广播表的创建以及使用

    创建表 , 关键词 : BROADCAST CREATE TABLE `financial_product_yjw` ( `ID` int(10) unsigned NOT NULL AUTO_INC ...

  4. 夺命雷公狗—angularjs—22—bind改指向和传参方式

    在angularjs中的传参的jquery的方式是极度相似的噢,而且还可以通过bind来改变指向 <!DOCTYPE html> <html lang="en" ...

  5. UINavigationController详解三(转)ToolBar

    原文出自:http://blog.csdn.net/totogo2010/article/details/7682641,特别感谢. 1.显示Toolbar  在RootViewController. ...

  6. nginx负载均衡的实现

    将一台nginx主机当作前端负载均衡服务器,后面通过交换机链接多台web服务器,提供html和php的web界面服务.通过配置前端负载均衡服务器,可以实现将html界面和php界面的分开访问,即htm ...

  7. scala的静态,单列模式

    package com.test.scala.test /** * 单例对象,这种对象不能提供构造函数 */ object SingleObject { private var lastnumber= ...

  8. Linux workqueue工作原理 【转】

    转自:http://blog.chinaunix.net/uid-21977330-id-3754719.html 转自:http://bgutech.blog.163.com/blog/static ...

  9. [转]Using the Group Pane to Repeat Page Titles

    转自:http://www.wiseowl.co.uk/blog/s148/group-pane-advanced-mode.htm Repeating Page Headers in Reporti ...

  10. 【Pro ASP.NET MVC 3 Framework】.学习笔记.1.主要语言特性

    C# 是一个富有特性的语言,并不是所有的程序员都熟悉本书依赖的所有特性.在本章,我们看看作为一个好的MVC程序员需要知道的C#特性. 1 C#主要特性 } 1.1 使用扩展方法 扩展方法 在你不能拥有 ...