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. 可压Navier-Stokes方程组的爆破现象

    在 Z.P. Xin, Blowup of smooth solutions to the compressible Navier-Stokes  equations with compact den ...

  2. Delphi WebService 需要注意 转

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://gang4415.blog.51cto.com/225775/251997 Web ...

  3. unity3d 雪与沙的渲染

    很简单的一个shader,跟着上一篇地形顺便弄的 方法就是基于物理的diffuse,再加上noise权重的specular 公式参考 JOURNEY JOURNEY中认为OrenNayar比较浪费,所 ...

  4. apache php gzip压缩输出的实现方法

    一.gzip介绍 gzip是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式.软件的作者是Jean-loup Gailly和Mark Adler.1992 ...

  5. 0ull 是什么意思?

    unsigned long long 类型的0 同理:#define     DEV_IIC1             ( (u64)1 <<  7ULL)    这个作用是64位中的第7 ...

  6. hdu 4403 枚举

    #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #inclu ...

  7. 开源跨平台的3D渲染软件

    http://www.blender.org/ KVM是Kernel-based Virtual Machine的缩写; http://kiwik.github.io/openstack/2013/1 ...

  8. 【PNG格式中文详解】

        技术文档(Document)   PNG格式 PNG是20世纪90年代中期开始开发的图像文件存储格式,其目的是企图替代GIF和TIFF文件格式,同时增加一些GIF文件格式所不具备的特性.流式网 ...

  9. struts2 最新S2-016-S2-017漏洞通杀struts2所有版本及修复方法

    详情查看http://zone.wooyun.org/content/5159 官方漏洞说明 http://struts.apache.org/release/2.3.x/docs/s2-016.ht ...

  10. spark-streaming-kafka包源码分析

    转载请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/5443789.html 最近由于使用sparkstreaming的同学需要对接到部门内部的的kafk ...