Problem Description
In ACM_DIY, there is one master called “Lost”. As we know he is a “-2Dai”, which means he has a lot of money.
  
Well, Lost use Ipad and IPhone to reward the ones who solve the following problem.
  
In this problem, we define F( n ) as :
  
Then Lost denote a function G(a,b,n,p) as

Here a, b, n, p are all positive integer!
If you could tell Lost the value of G(a,b,n,p) , then you will get one Ipad and one IPhone!
 
Input
The first line is one integer T indicates the number of the test cases. (T <= 100)
Then for every case, only one line containing 4 positive integers a, b, n and p.
(1 ≤a, b, n, p≤2*109 , p is an odd prime number and a,b < p.)
 
Output
Output one line,the value of the G(a,b,n,p) .
 
Sample Input
4
2 3 1 10007
2 3 2 10007
2 3 3 10007
2 3 4 10007
 
Sample Output
40
392
3880
9941
 
Author
AekdyCoin
 
Source
 
Recommend
notonlysuccess   |   We have carefully selected several similar problems for you:  3805 3800 3801 3803 3804
【分析】
不知道怎么回事...跟网上的程序对拍了好像没错,怎么过不了...应该是有些比较坑的点....
不错的一道题目,前面的两项不说了,后面的那一项可以把二次方弄进去,然后变成一个用韦达定理做一个逆运算得到特征方程。
然后就有递推式了,然后就可以矩阵加速了。
然后因为幂实在是太大了,然后上降幂大法,然后没了。
其实还涉及到二次剩余的理论,如果前面没有那两个式子答案就错了....因为刚好是那两个式子,让不能用降幂大法的情况变成0...
 /*
五代李煜
《相见欢·林花谢了春红》
林花谢了春红,太匆匆。无奈朝来寒雨晚来风。
胭脂泪,相留醉,几时重。自是人生长恨水长东。
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#define LOCAL
const int MAXN = + ;
const int INF = 0x7fffffff;
using namespace std;
typedef long long ll;
ll mod;//代表取模的数字
ll check, a, b, n, p;
struct Matrix{
ll num[][];
//Matrix(){memset(num, 0, sizeof(num));}
};
//为了防止和第一种矩阵乘法搞混
Matrix Mod(Matrix A, Matrix B, ll k){
if (k == ){//代表两种不同的乘法
Matrix c;
memset(c.num, , sizeof (c.num));
for (ll i = ; i <= ; i++)
for (ll j = ; j <= ; j++)
for (ll k = ; k <= ; k++){
ll tmp = (A.num[i][k] * B.num[k][j]);
if (check) tmp %= (p - );
c.num[i][j] += tmp;
if (check) c.num[i][j] %= (p - );
}
//一旦大于了p-1代表当前出现的斐波那契数列会大于phi(p),可以使用降幂大法
if ((c.num[][] + c.num[][]) > (p - )) check = ;
return c;
}else if (k == ){
Matrix C;
memset(C.num, , sizeof(C.num));
for (ll i = ; i <= ; i++)
for (ll j = ; j <= ; j++)
for (ll k = ; k <= ; k++) {
C.num[i][j] += (A.num[i][k] * B.num[k][j]) % p;
C.num[i][j] = ((C.num[i][j] % p) + p) % p;
}
return C;
}
}
//得到第x位的斐波那契数,也就是获得指数
Matrix Matrix_pow(Matrix A, ll x, ll k){
if (x == ) return A;
Matrix tmp = Matrix_pow(A, x / , k);
if (x % == ) return Mod(tmp, tmp, k);
else return Mod(Mod(tmp, tmp, k), A, k);
}
ll get(ll x){
if (x == ) return ;
else if (x == ) return ;
Matrix A, B;
A.num[][] = ; A.num[][] = ;
A.num[][] = ; A.num[][] = ;
x--;//为真实的需要乘的次数
if (x == ) return ;
B = Matrix_pow(A, x, );
if (B.num[][] + B.num[][] > (p - )) check = ;
if (check == ) return B.num[][] + B.num[][];
else return (B.num[][] + B.num[][]) % (p - ) + p - ;
}
//有了a,b,pos就可进行矩阵加速了
ll cal(ll a, ll b, ll pos){
if (pos == ) return % p;
else if (pos == ) return ( * (a + b)) % p;
Matrix A;
A.num[][] = ( * (a + b)) % p; A.num[][] = (((-(a - b) * (a - b)) % p) + p) % p;
A.num[][] = ; A.num[][] = ;
pos--;
Matrix B;
B = Matrix_pow(A, pos, );
return (B.num[][] * A.num[][]) % p + (B.num[][] * ) % p;
}
ll pow(ll a, ll b){
if (b == ) return % p;
if (b == ) return a % p;
ll tmp = pow(a, b / );
if (b % == ) return (tmp * tmp) % p;
else return (((tmp * tmp) % p) * a) % p;
} int main(){
int T;
scanf("%d", &T);
while (T--){
//for (int i = 1; i ,=)
scanf("%lld%lld%lld%lld", &a, &b, &n, &p);
check = ;//判断f(n)是否大于p
ll pos = get(n);
ll Ans = cal(a, b, pos);
ll f1, f2;
f1 = (pow(a, (p - ) / ) + ) % p;
f2 = (pow(b, (p - ) / ) + ) % p;
Ans = (((f1 * f2) % p) * Ans) % p;
printf("%lld\n", Ans);
}
//p = 0x7fffffff;
//printf("%lld", get(5));
//for (int i = 0; i <= 10; i++) printf("%lld\n", get(i));
return ;
}

【HDU3802】【降幂大法+矩阵加速+特征方程】Ipad,IPhone的更多相关文章

  1. Ipad,IPhone(矩阵求递推项+欧拉定理)

    Ipad,IPhone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. HDU 5564 Clarke and digits 状压dp+矩阵加速

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 求长度在[L,R]范围,并且能整除7的整数的总数. 题解: 考虑最原始的想法: dp[ ...

  3. 【BZOJ3884】【降幂大法】上帝与集合的正确用法

    Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“元” ...

  4. 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)

    Yet Another Number Sequence Description Everyone knows what the Fibonacci sequence is. This sequence ...

  5. 【HDU 3483】 A Very Simple Problem (二项式展开+矩阵加速)

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  6. C++矩阵加速经典题目:Warcraft III 守望者的烦恼 [vijos 1067]

    Warcraft III 守望者的烦恼 背景 守望者-warden,长期在暗夜精灵的的首都艾萨琳内担任视察监狱的任务,监狱是成长条行的,守望者warden拥有一个技能名叫"闪烁", ...

  7. LuoGu P1939 【模板】矩阵加速(数列)

    板子传送门 矩阵快速幂学完当然要去搞一搞矩阵加速啦 (矩阵加速相对于矩阵快速幂来说就是多了一个构造矩阵的过程) 关于怎样来构造矩阵,这位大佬讲的很好呢 构造出矩阵之后,我们再去用矩阵快速幂乘出来,取[ ...

  8. Luogu P3390 【模板】矩阵快速幂&&P1939 【模板】矩阵加速(数列)

    补一补之前的坑 因为上次关于矩阵的那篇blog写的内容太多太宽泛了,所以这次把一些板子和基本思路理一理 先看这道模板题:P3390 [模板]矩阵快速幂 首先我们知道矩阵乘法满足结合律而不满足交换律的一 ...

  9. P1939【模板】矩阵加速(数列)

    P1939[模板]矩阵加速(数列)难受就难受在a[i-3],这样的话让k=3就好了. #include<iostream> #include<cstdio> #include& ...

随机推荐

  1. php-Mysql示例1

  2. leetcode https://oj.leetcode.com/problems/jump-game-ii/

    1.超时的,效率太低 public class Solution { public int jump(int[] A) { int len=A.length; int d[]=new int[len] ...

  3. CSS3 概览 更新时间 2014-0412-1317

    CSS3 概览 CSS3可以划分为:文字.边框模型.背景.动画等. CSS3颜色模块 CSS2.1的时候可以使用4种颜色方式,直接使用颜色名,如 redRGB值,如 rgb(0,90,255)RGB百 ...

  4. 【原】centos6.5下hadoop cdh4.6 安装

    1.架构准备:      namenode 10.0.0.2      secondnamenode 10.0.0.3      datanode1 10.0.0.4      datanode2 1 ...

  5. 【转】SVN linux命令及 windows相关操作(三)

    TortoiseSVN是windows下其中一个非常优秀的SVN客户端工具.通过使用它,我们可以可视化的管理我们的版本库.不过由于它只是一个客户端,所以它不能对版本库进行权限管理. TortoiseS ...

  6. HIVE自定义函数 UDF

    自定义my_md5hash 具体hive源码怎么调用看我另外一篇博客 package udf.hive.myudf; import org.apache.commons.codec.digest.Di ...

  7. UIView与CALayer的区别,很详细

    研 究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不多,看得人云山雾罩,感觉,写那篇东西的人, ...

  8. eclipse运行项目特别慢,出现Java heap space溢出

    在eclipse中可用为JVM设置参数:Window-->Preferences-->Java-->Installed JREs然后选中你安装的jre-->Edit--> ...

  9. GWT中实现跳转及不同entrypoint怎么互相访问

    怎么跳转? 跳转这个概念这里指的是从一个web页面跳转到另一个web页面,如果我们使用gwt来开发web,很自然的我们会想到怎么从一个gwt做的页面跳转到另一个gwt做的页面. 但从网上的gwt例子来 ...

  10. Linux Resin 安装

    1 Resin 下载 Resin 官方下载网址. 最新版下载 resin-4.0.36.tar.gz(免费版) resin 安装须要提前配置好jdk.配置jdk请看上面文章 2 Resin 安装 (1 ...