M斐波那契数列

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

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
 
Source
 题意:给你 a ,b,n 求f[n];
 题解:矩阵快速幂+快速幂+欧拉函数
 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll a,b,n;
struct matrix
{
ll m[][];
} ans,exm;
ll phi(ll nn)
{
ll i,rea=nn;
for(i=;i*i<=nn;i++)
{
if(nn%i==)
{
rea=rea-rea/i;
while(nn%i==)
nn/=i;
}
}
if(nn>)
rea=rea-rea/nn;
return rea;
}
ll zha=phi(mod);
struct matrix matrix_mulit(struct matrix aa, struct matrix bb)
{
struct matrix there;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
there.m[i][j]=;
for(int k=; k<; k++)
there.m[i][j]=(there.m[i][j]+aa.m[i][k]*bb.m[k][j]%zha)%zha;
}
}
return there;
};
ll matrix_quick(ll gg)
{
exm.m[][]=exm.m[][]=exm.m[][]=;
exm.m[][]=;
ans.m[][]=ans.m[][]=;
ans.m[][]=ans.m[][]=;
while(gg)
{
if(gg&)
{
ans=matrix_mulit(ans,exm);
}
exm=matrix_mulit(exm,exm);
gg>>=;
}
return ans.m[][];
}
ll quickmod(ll aa,ll bb)
{
ll re=;
while(bb)
{
if(bb&)
re=(re*aa)%mod;
aa=(aa*aa)%mod;
bb>>=;
}
return re;
} int main()
{
while(scanf("%I64d %I64d %I64d",&a,&b,&n)!=EOF)
{
if(n==)
printf("%I64d\n",a);
else
{
if(n==)
printf("%I64d\n",b);
else
{
printf("%I64d\n",quickmod(a,matrix_quick(n-)+zha)*quickmod(b,matrix_quick(n-)+zha)%mod);
}
}
}
return ;
}

HDU 4549 矩阵快速幂+快速幂+欧拉函数的更多相关文章

  1. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  2. poj3696 快速幂的优化+欧拉函数+gcd的优化+互质

    这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...

  3. 快速切题 sgu102.Coprimes 欧拉函数 模板程度 难度:0

    102. Coprimes time limit per test: 0.25 sec. memory limit per test: 4096 KB For given integer N (1&l ...

  4. HDU 1286:找新朋友(欧拉函数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...

  5. HDU 6088 Rikka with Rock-paper-scissors(NTT+欧拉函数)

    题意 \(n\) 局石头剪刀布,设每局的贡献为赢的次数与输的次数之 \(\gcd\) ,求期望贡献乘以 \(3^{2n}\) ,定义若 \(xy=0\) 则,\(\gcd(x,y)=x+y\) 思路 ...

  6. (hdu step 7.2.2)GCD Again(欧拉函数的简单应用——求[1,n)中与n不互质的元素的个数)

    题目: GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. hdu 1286 找新朋友 (容斥原理 || 欧拉函数)

    Problem - 1286 用容斥原理做的代码: #include <cstdio> #include <iostream> #include <algorithm&g ...

  8. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

  9. 找新朋友---hdu1286(欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286 欧拉函数:对正整数n,欧拉函数是求少于n的数中与n互质的数的数目: 素数(质数)指在一个大于1的 ...

  10. POJ_2480 Longge's problem【积性函数+欧拉函数的理解与应用】

    题目: Longge is good at mathematics and he likes to think about hard mathematical problems which will ...

随机推荐

  1. java后台接受web前台传递的数组参数

    前台发送:&warning_type[]=1,2 &warning_type=1,2 后台接收:(@RequestParam(value = "param[]") ...

  2. CsvHelper文档-2读

    CsvHelper文档-2读 这个库默认不需要做任何设置就可以很容易的使用它.如果你的类属性名称直接匹配csv的标题名称,那么可以按照下面的实例来用: (以下所有的代码都需要引用using csvhe ...

  3. 解决mac OS 10.9 下python 在terminal下崩溃的问题

    Python 2.7.6 release candidate 1 was released on October 26, 2013. This is a 2.7 series bugfix relea ...

  4. [C++] in-class initializer

    C++11 introduced serveral contructor-related enhancements including: Class member initializers Deleg ...

  5. canvas学习(四):高级属性

    一:阴影 示例:绘制一个带有阴影的正方形 var canvas = document.getElementById("myCanvas") var ctx = canvas.get ...

  6. kmeans算法理解及代码实现

    github:kmeans代码实现1.kmeans代码实现2(包含二分k-means) 本文算法均使用python3实现 1 聚类算法   对于"监督学习"(supervised ...

  7. 在mysql启用远程连接

    1.在ubuntu下面安装mysql. apt-get install mysql-server mysql-client -y 2.修改/etc/mysql/my.cnf文件. #bind-addr ...

  8. alpha阶段个人总结(201521123034陈凯欣)

    一.个人总结 第 0 部分:基本数据结构和算法问题 大二的时候上过数据结构课,感觉自己没有学的太深入,就如之前结对编程时候四则运算有用到的二叉树来解决问题,对于二叉树就有个模糊的概念,实际动手操作起来 ...

  9. python爬虫从入门到放弃(五)之 正则的基本使用(转)

    什么是正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是 事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符”,这个“规则字符” 来表达对字符的一种过滤逻辑. 正则并不是pyth ...

  10. phpcms免登录cookies设置方案

    PHPCMS的SESSION时间长一些的解决办法修改两个文件: phpsso_server/caches/configs/system.php里的 'session_ttl' => 999999 ...