求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} - \sqrt{b})^{2F_n}] (mod p)$

左边可以看出是欧拉判定准则,那么只有当a,b其中一个满足是模p下的非二次剩余时G()为0。

右边的式子可以先把平方放进去,发现这个已经是通项公式了,那么$a+b+\sqrt{ab}$和$a+b-\sqrt{ab}$就是它的特征根了,反代回二阶特征方程,得到递推式的两个系数$2(a+b)$,$-(a-b)^2$,然后可以使用矩阵快速幂了,注意其项数是$F(n)$,指数循环节,欧拉降幂,其矩阵快速幂的过程中模p-1就行了。

/** @Date    : 2017-10-11 22:30:33
* @FileName: HDU 3802 斐波那契特征方程解递推式 矩阵快速幂.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; LL mod; LL fpow(LL a, LL n)
{
LL res = 1LL;
while(n)
{
if(n & 1LL) res = (res * a % mod + mod) % mod;
a = (a * a % mod + mod) % mod;
n >>= 1LL;
}
return res;
} struct matrix
{
LL mat[2][2];
matrix(){MMF(mat);}
void id(){
mat[0][0] = mat[1][1] = 1LL;
}
matrix operator *(const matrix &b){
matrix c;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
for(int k = 0; k < 2; k++)
{
c.mat[i][j] = (c.mat[i][j] + mat[i][k] * b.mat[k][j] % mod) % mod;
}
return c;
}
matrix operator ^(LL n){
matrix res;
matrix a = *this;
res.id();
while(n)
{
if(n & 1LL)
res = res * a;
a = a * a;
n >>= 1LL;
}
return res;
}
}; LL fib(LL n)
{
mod--;
matrix T;
T.mat[0][0] = 1LL, T.mat[0][1] = 1LL;
T.mat[1][0] = 1LL, T.mat[1][1] = 0LL;
matrix tmp = (T^(n-1));
LL ans = ((tmp.mat[0][0] + tmp.mat[1][0])%mod + mod) % mod;
mod++;
return ans;
} LL fun(LL a, LL b, LL n)
{
LL ans = (fpow(a, (mod - 1)>>1) + 1LL) * (fpow(b, (mod-1)>>1) + 1LL) % mod;//注意这里也要取模...
if(ans == 0)
return 0;
if(n < 2)
return (a + b) * 2LL % mod * ans % mod;
LL e = fib(n);
//cout << e << endl;
if(e == 0)
e = mod - 1;
matrix A;
A.mat[0][0] = (a + b) * 2LL % mod, A.mat[0][1] = 1LL;
A.mat[1][0] = (b - a) * (a - b) % mod, A.mat[1][1] = 0LL;
A = A^(e - 1);
ans = (ans * ((a + b) * 2LL % mod * A.mat[0][0] + A.mat[1][0] * 2LL % mod) % mod) % mod;
while(ans < 0)
ans += mod;
return ans;
}
int main()
{
int T;
cin >> T;
while(T--)
{
LL a, b, n;
scanf("%lld%lld%lld%lld", &a, &b, &n, &mod);
LL ans = fun(a, b, n);
printf("%lld\n", ans);
}
return 0;
}

HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识的更多相关文章

  1. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  2. HDU 5863 cjj's string game ( 16年多校10 G 题、矩阵快速幂优化线性递推DP )

    题目链接 题意 : 有种不同的字符,每种字符有无限个,要求用这k种字符构造两个长度为n的字符串a和b,使得a串和b串的最长公共部分长度恰为m,问方案数 分析 : 直觉是DP 不过当时看到 n 很大.但 ...

  3. 矩阵快速幂(queue递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 Queuing Time Limit: 10000/5000 MS (Java/Others)    Me ...

  4. Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)

    Problem C. Numbers This contest is open for practice. You can try every problem as many times as you ...

  5. HDU 1757 矩阵快速幂加速递推

    题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...

  6. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  7. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  8. HDU 4686 矩阵快速幂 Arc of Dream

    由式子的性质发现都是线性的,考虑构造矩阵,先有式子,a[i] = ax * a[i-1] + ay; b[i] = bx*b[i-1] +by; a[i]*b[i] = ax*bx*a[i-1]*b[ ...

  9. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

随机推荐

  1. “吃神么,买神么”的第一个Sprint计划(第七天)

    “吃神么,买神么”项目Sprint计划 ——5.25  星期一(第五天)立会内容与进度 摘要: 所有的部件都完成,在贴每个人负责的部件时发现很多问题,很多网页布局的运用不熟练,一部分的div会跑位置~ ...

  2. 浅学html

    数据库web端需要了解html等语言,就初浅学习一下 <!DOCTYPE html> <html> <head> <meta charset="ut ...

  3. NET Core 部署到 Windows服务

    https://www.cnblogs.com/linezero/p/5159927.html https://www.cnblogs.com/emrys5/p/nssm-netcore.html h ...

  4. Workstation和Virtualbox的虚拟机磁盘扩容方式.

    1. 虚拟机磁盘管理, 更改磁盘格式是一个场景 还有一个场景是 硬盘空间不够了 需要扩充. 方法主要有两个. 如果是workstation的的虚拟机. 并且没有快照 可以直接GUI操作 如下图: 虚拟 ...

  5. Collection List区别

    Collection是无序的,比如一大群人在广场上,你不可能说某某人是第一个,某某人是第二个 List是有序的,比如一群人从高到矮排了队,你就能说这人是第一个,这人是最后一个 因此Collection ...

  6. 半夜思考之查漏补缺 , Spring 中的 Bean 继承机制

    这里的继承 , 不是 Java 中的继承 , 下面就总结下 Bean继承与Java继承的区别: Spring 中的子类 Bean 和父 Bean 可以是不同类型 , 但是 Java 中的继承则可保证子 ...

  7. SPOJ_LCS

    经典题目,求两个串的最长公共子串. 是这样来做的. 以第一个串构造SAM,第二个串在自动机上跟新一遍就可以了. 更新的过程是这样的,假设当前到达的状态点为x(初始状态为0点),下一个字符是c,如果当前 ...

  8. 2018 桂林ccpc现场赛 总结

    Day 0 5个小时的火车,坐的昏昏欲睡.桂林站出来没有地铁,而是出租车排成长队依次上车,也算是某种意义上的地铁吧.到了酒店才发现学校那边又给我们换了,又拖着行李找新的酒店,途中路过一家餐馆,所有人都 ...

  9. Sort HDU - 5884(优先队列+二分)

    Sort Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. Getting logback and slf4j to work in JBoss AS 7

    As usual, it has to do with classloading and that JBoss internally also uses slf4j and logback. As e ...