求$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. ThoughtWorks.QRCode类库

    ThoughtWorks.QRCode一个二维码生成类库.

  2. 安装/卸载 修改Config

    参考地址:https://docs.microsoft.com/zh-cn/nuget/create-packages/source-and-config-file-transformations

  3. fx投影效果分离

    虽然忙着花花二期三期bug.bug  ing,修改等待中突然看见一张logo.文字下面的阴影图,如下,就满脑子在想阴影到底咋做的.. 七拼八凑的尝试后大体样子是有,终究没有上图那种字体轮廓的阴影... ...

  4. 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)

    步骤: 1.运行--〉cmd:打开cmd命令框 2.在命令行里定位到InstallUtil.exe所在的位置 InstallUtil.exe 默认的安装位置是在C:/Windows/Microsoft ...

  5. testNg-build.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?> <proje ...

  6. gitlab修改root密码

    在root用户下,执行 [root@localhost gitlab]# sudo gitlab-rails console production -------------------------- ...

  7. [微软官方]SQLSERVER的兼容级别

    ALTER DATABASE (Transact-SQL) 兼容级别 https://docs.microsoft.com/zh-cn/sql/t-sql/statements/alter-datab ...

  8. SpringBoot(十)_springboot集成Redis

    Redis 介绍 Redis是一款开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存也可持久化的日志型.Key-Value高性能数据库. 数据模型 Redis 数据模型不仅与关系数据 ...

  9. LeetCode 717. 1-bit and 2-bit Characters

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  10. vue element 新增、编辑类Dialog公用函数

    调用 <el-button type="primary" class="my-button" size="small" :loadin ...