矩阵快速幂,欧拉定理。

$g(n)$递推式:$g(n)=5g(n-1)+5g(n-2)-g(n-3)$,可以构造矩阵快速求递$n$项,指数很大,可以利用欧拉定理降幂。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} LL MOD; struct Matrix
{
long long A[][];
int R, C;
Matrix operator*(Matrix b);
}; Matrix X, Y, Z; Matrix Matrix::operator*(Matrix b)
{
Matrix c;
memset(c.A, , sizeof(c.A));
int i, j, k;
for (i = ; i <= R; i++)
for (j = ; j <= b.C; j++)
for (k = ; k <= C; k++)
c.A[i][j] = (c.A[i][j] + (A[i][k] * b.A[k][j]) % MOD) % MOD;
c.R = R; c.C = b.C;
return c;
} void init()
{
memset(X.A, , sizeof X.A);
memset(Y.A, , sizeof Y.A);
memset(Z.A, , sizeof Z.A); Y.R = ; Y.C = ;
for (int i = ; i <= ; i++) Y.A[i][i] = ; X.R = ; X.C = ;
X.A[][]=-;
X.A[][]=; X.A[][]=;
X.A[][]=; X.A[][]=; Z.R = ; Z.C = ;
Z.A[][]=; Z.A[][]=; Z.A[][]=;
} LL work(LL tt)
{
if(tt==) return ;
if(tt==) return ;
if(tt==) return ; tt=tt-; init();
while (tt)
{
if (tt % == ) Y = Y*X;
tt = tt >> ;
X = X*X;
}
Z = Z*Y;
return Z.A[][];
} int T;
LL n,x,y,s; LL phi(LL x)
{
LL res=x,a=x;
for(int i=;i*i<=a;i++)
{
if(a%i==)
{
res=res/i*(i-);
while(a%i==) a/=i;
}
}
if(a>) res=res/a*(a-);
return res;
} LL pow(LL a,LL b,LL mod)
{
LL c=;
while(b!=)
{
if(b%==) c=(c*a)%mod,b--;
else a=(a*a)%mod,b=b/;
}
return c;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld%lld",&n,&y,&x,&s);
MOD=phi(s+);
LL gn=work(n*y)+MOD;
LL ans=pow(x,gn,s+);
printf("%lld\n",ans);
}
return ;
}

HDU 5895 Mathematician QSC的更多相关文章

  1. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  2. hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. hdu 5895 广义Fibonacci数列

    Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  4. hdu-5895 Mathematician QSC(数学)

    题目链接: Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Jav ...

  5. hdu 5895(矩阵快速幂+欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895 f(n)=f(n-2)+2*f(n-1) f(n)*f(n-1)=f(n-2)*f(n-1)+2 ...

  6. 2016.9.18 --- Shenyang ol

    1001 Resident Evil 1002 List wants to travel 1003 hannnnah_j’s Biological Test 1004 Mathematician QS ...

  7. HDU 5900 QSC and Master 区间DP

    QSC and Master Problem Description   Every school has some legends, Northeastern University is the s ...

  8. HDU 5900 QSC and Master (区间DP)

    题目链接   http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意:给出序列$A_{i}.key$和$A_{i}.value$,若当前相邻的两个数$A_{ ...

  9. HDU 5900 - QSC and Master [ DP ]

    题意: 给n件物品,有key和value 每次可以把相邻的 GCD(key[i], key[i+1]) != 1 的两件物品,问移除的物品的总value最多是多少 key : 1 3 4 2  移除3 ...

随机推荐

  1. ssh的public key的使用

    1.在客户端Xftp的工具栏tools->Key Generation Parameters 弹出会话窗口,在key type中选择RSA

  2. Linux下获取硬盘使用情况

    Linux下获取硬盘使用情况[总结] 1.前言 在嵌入式设备中,硬盘空间非常有限,在涉及到经常写日志的进程时候,需要考虑日志的大小和删除,不然很快就硬盘写满,导致日志程序崩溃.为了捕获硬盘写满的异常场 ...

  3. thinkphp实现自动登录

    网页上经常有一些自动登录的checkbox,勾选后,下次进入该网站,无需登录,即可执行一些需要登录才能执行的操作.上班无事,用thinkphp做了下 1 下面是一个很普通的form表单,有一个chec ...

  4. 【Yom框架】漫谈个人框架的设计之二:新的IRepository接口+搜索和排序解耦(+基于Castle实现)

    经过了上篇IRepository和IRepository<T>的讨论[文章地址为:http://www.cnblogs.com/yomho/p/3296759.html] 我选择了IRep ...

  5. HTTP状态码的意义

    100系列码 从100到199范围的HTTP状态码是信息报告码.基于各种原因考虑,大多数情况下我们是 很少看见这些代码的.首先,如果一个浏览器尝试访问一个网站,而网站返回这些代码时,它们往往都不会显示 ...

  6. 单一职责原则SRP

    定义: There should nerver be more then one reason for a class to change. 优点: 1.类的复杂性降低,实现什么职责都有清晰明确的定义 ...

  7. 拦截所有AJAX调用,重点处理服务器异常

    拦截所有AJAX调用,重点处理服务器异常 背景 上篇文章http://www.cnblogs.com/happyframework/p/3241063.html介绍了如何以AOP的形式处理服务器异常, ...

  8. vim 多行同时输入,且输入数值递增

    很有用的命令. 很给力的说. http://vim.wikia.com/wiki/Making_a_list_of_numbers 我在 html中需要增加新的标签的时候,就有用到过. 原来的html ...

  9. 【OpenMesh】使用迭代器和循环机

    原文出处: http://openmesh.org/Documentation/OpenMesh-Doc-Latest/tutorial.html 这个例子展现: 如何使用迭代器 如何使用循环机 这个 ...

  10. Unable to start T-SQL Debugging. Could not connect to the computer ‘.’

    Unable to start T-SQL Debugging. Could not connect to the computer ‘.’ 在Win7上面使用SSMS连接到SQL Server使用D ...