HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3024 Accepted Submission(s): 930
F[0] = a
F[1] = b
F[n] = F[n-1] * F[n-2] ( n > 1 )
现在给出a, b, n,你能求出F[n]的值吗?
每组数据占一行,包含3个整数a, b, n( 0 <= a, b, n <= 10^9 )
60
#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斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理的更多相关文章
- hdu4549 M斐波那契数列 矩阵快速幂+快速幂
M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n]的 ...
- 51nod1242 斐波那契数列 矩阵快速幂
1242 斐波那契数列的第N项 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 #include<stdio.h> #define mod 100000000 ...
- POJ3070 斐波那契数列 矩阵快速幂
题目链接:http://poj.org/problem?id=3070 题意就是让你求斐波那契数列,不过n非常大,只能用logn的矩阵快速幂来做了 刚学完矩阵快速幂刷的水题,POJ不能用万能头文件是真 ...
- 洛谷P1962 斐波那契数列(矩阵快速幂)
题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ 2 且 n 为整数) 题目描述 请 ...
- hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17171 Accepted: 11999 Descr ...
- 洛谷- P1306 斐波那契公约数 - 矩阵快速幂 斐波那契性质
P1306 斐波那契公约数:https://www.luogu.org/problemnew/show/P1306 这道题目就是求第n项和第m项的斐波那契数字,然后让这两个数求GCD,输出答案的后8位 ...
- [bzoj 1409] Password 矩阵快速幂+欧拉函数
考试的时候想到了矩阵快速幂+快速幂,但是忘(bu)了(hui)欧拉定理. 然后gg了35分. 题目显而易见,让求一个数的幂,幂是斐波那契数列里的一项,考虑到斐波那契也很大,所以我们就需要欧拉定理了 p ...
- poj3070 (斐波那契,矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9630 Accepted: 6839 Descrip ...
随机推荐
- angular 依赖注入
依赖注入 依赖注入(DI)是一个经典的设计模式, 主要是用来处理组件如何获得依赖的问题.关于DI,推荐阅读Martin Flower的文章(http://martinfowler.com/art ...
- RMAN连接数据库
连接本地数据库: 方法1: C:\Documents and Settings\Administrator>set oracle_sid=jssweb C:\Documents and Sett ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON SetGray
zw版[转发·台湾nvp系列Delphi例程]HALCON SetGray SetGray_Delphi.PNG unit Unit1;interfaceuses Windows, Messages, ...
- POJ - 2041Unreliable Message
这里的算法非常简单,就是“模拟”,注意编写每个传令官的算法时先分开测试,放在一起就会混淆. POJ - 2041Unreliable Message Time Limit: 1000MS Memory ...
- 微软Sql server analysis service数据挖掘技术
最新在一个项目中要求用到微软SSAS中的数据挖掘功能,虽然以前做项目的时候也经常用到SSAS中的多维数据集 (就是CUBE),但是始终没有对SSAS中的数据挖掘功能进行过了解.所以借着项目需求这股东风 ...
- Javascript Regexp match and replace
# add a new article reference to database function addnewpub() { var year = $("input#year" ...
- Install DBD::mysql for Perl in XAMPP in Mac , solving errors
我不知道 why,在 Mac 安装 DBI::mysql 总会报错 我为了给 cgi-bin 添加 mysql-perl 数据库支持,也是够麻烦的 make sure that mysql and m ...
- DateTimeUtil 工具类,android 和 java 通用
import java.sql.Date;import java.text.SimpleDateFormat; public class DateTimeUtil { public final cla ...
- Yahoo! s4和Twitter storm的粗略比较
- [UML]转:浅谈UML的概念和模型之UML九种图
转自:http://blog.csdn.net/jiuqiyuliang/article/details/8552956 目录: UML的视图 UML的九种图 UML中类间的关系 上文我们介绍了,UM ...