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. Visual Studio 2013新功能

    微软打破了Visual Studio两年升级一次的传统,Visual Studio 2012发布还不足一年,微软就计划发布了Visual Studio 2013了.在今天的TechEd大会上,微软宣布 ...

  2. UVA 11762 Race to 1(记忆化+期望)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20869 [思路] DP+期望. 设f[x]表示从x转移到1的期望操 ...

  3. linux平台MongoDB数据库安装

    跟Ruiy哥一起玩转吧; <一,初始化玩转MongoDB> 1,关闭SElinux(Ruiy哥根据经验知红帽的SElinux架设就是个错误,还记得不管啥结构首先要关闭的就是它); 2,设置 ...

  4. 干货!如何正确使用Git Flow

    我们已经从SVN 切换到Git很多年了,现在几乎所有的项目都在使用Github管理, 本篇文章讲一下为什么使用Git, 以及如何在团队中正确使用. Git的优点 Git的优点很多,但是这里只列出我认为 ...

  5. poj 1218 THE DRUNK JAILER【水题】

    THE DRUNK JAILER Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25124   Accepted: 1576 ...

  6. Web移动端Fixed布局的解决方案

    移动端业务开发,iOS 下经常会有 fixed 元素和输入框(input 元素)同时存在的情况. 但是 fixed 元素在有软键盘唤起的情况下,会出现许多莫名其妙的问题. 这篇文章里就提供一个简单的有 ...

  7. C++ 标准时间线

    Herb Sutter在他的博客上贴出了一个C++的timeline,如下所示:

  8. js学习笔记之包装对象

    JavaScript包装对象 近日有时间,闲下来好好学习原生js JavaScript是一门面向对象语言,使用"."就可以访问对象的属性和方法,而基本类型(null, undefi ...

  9. UVA 10465 Homer Simpson(dp + 完全背包)

    Problem C: Homer Simpson Time Limit: 3 seconds Memory Limit: 32 MB Homer Simpson, a very smart guy, ...

  10. linux 调度器配制参数

    http://blog.csdn.net/wudongxu/article/details/8574753 参数位置: /proc/sys/kernel/ 编绎内核时参数 [root@monitor ...