HDU 3306 Another kind of Fibonacci(快速幂矩阵)

题目链接

构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余。

#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define MOD 10007
#define LL __int64
LL p[4][4],mat[4][4];
int qmod(int n)
{
LL c[4][4];
int i,j,k;
while(n)
{
if(n&1)
{
memset(c,0,sizeof(c));
for(i = 0;i < 4;i ++)
{
for(j = 0;j < 4;j ++)
{
for(k = 0;k < 4;k ++)
{
c[i][j] += mat[i][k]*p[k][j];
c[i][j] %= MOD;
}
}
}
memcpy(mat,c,sizeof(mat));
}
memset(c,0,sizeof(c));
for(i = 0;i < 4;i ++)
{
for(j = 0;j < 4;j ++)
{
for(k = 0;k < 4;k ++)
{
c[i][j] += p[i][k]*p[k][j];
c[i][j] %= MOD;
}
}
}
memcpy(p,c,sizeof(p));
n >>= 1;
}
return (mat[0][1] + mat[0][0] + mat[0][2] + mat[0][3])%MOD;
}
int main()
{
LL x,y,n;
int i,j;
while(scanf("%I64d%I64d%I64d",&n,&x,&y)!=EOF)
{
memset(p,0,sizeof(p));
p[0][0] = p[0][1] = 1;
p[1][1] = x*x;
p[1][2] = y*y;
p[1][3] = 2*x*y;
p[2][1] = 1;
p[3][1] = x;
p[3][3] = y;
for(i = 0;i < 4;i ++)
{
for(j = 0;j < 4;j ++)
{
if(i == j)
mat[i][j] = 1;
else
mat[i][j] = 0;
}
}
printf("%d\n",qmod(n));
}
return 0;
}

HDU 3306 Another kind of Fibonacci(快速幂矩阵),布布扣,bubuko.com

时间: 2024-12-28 12:42:06

HDU 3306 Another kind of Fibonacci(快速幂矩阵)的相关文章

hdu 3306 Another kind of Fibonacci

点击此处即可传送hdu 3306 **Another kind of Fibonacci** Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2005 Accepted Submission(s): 787 Problem Description As we all known , the Fibonacci series : F(0) =

HDU 4686 Arc of Dream(快速幂矩阵)

题目链接 再水一发,构造啊,初始化啊...wa很多次啊.. #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MOD 1000000007 #define

hdu 4965 矩阵快速幂 矩阵相乘性质

Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 170    Accepted Submission(s): 99 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a gir

hdu 4704 费马小定理+快速幂

题意就是:做整数拆分,答案是2^(n-1) 由费马小定理可得:2^n % p = 2^[ n % (p-1) ]  % p 当n为超大数时,对其每个数位的数分开来加权计算 当n为整型类型时,用快速幂的方法求解 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; const in

矩阵十题【四】 HDU 3306 Another kind of Fibonacci

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3306 题目大意:A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2):给定三个值N,X,Y求S(N):S(N) = A(0)^2 +A(1)^2+--+A(n)^2. 学了这几题,还是不太很懂,后来看题解,渐渐也是懂了一点. 题目的意思是求出A(0)^2 +A(1)^2+--+A(n)^2 考虑1*4 的矩阵[s[n-2]

[ACM] hdu 3923 Invoker (Poyla计数,快速幂运算,扩展欧几里得或费马小定理)

Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful. In

HDU 4965 Fast Matrix Caculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 )

HDU 4965 Fast Matrix Calculation ( 矩阵乘法 + 矩阵快速幂 + 矩阵乘法的结合律 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define MAX_SIZE 1001 #define CLR( a, b ) memset( a, b, sizeof(a) ) #define MOD 6 typedef long lo

HDU - 5187 zhx&#39;s contest(快速幂+快速乘法)

作为史上最强的刷子之一,zhx的老师让他给学弟(mei)们出n道题.zhx认为第i道题的难度就是i.他想要让这些题目排列起来很漂亮. zhx认为一个漂亮的序列{ai}下列两个条件均需满足. 1:a1..ai是单调递减或者单调递增的. 2:ai..an是单调递减或者单调递增的. 他想你告诉他有多少种排列是漂亮的.因为答案很大,所以只需要输出答案模p之后的值. Input Multiply test cases(less than 10001000). Seek EOF as the end of

Number Sequence(快速幂矩阵)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 131753    Accepted Submission(s): 31988 Problem Description A number sequence