HDU 5895 Mathematician QSC
矩阵快速幂,欧拉定理。
$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的更多相关文章
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- hdu 5895 Mathematician QSC 指数循环节+矩阵快速幂
Mathematician QSC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- hdu 5895 广义Fibonacci数列
Mathematician QSC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- hdu-5895 Mathematician QSC(数学)
题目链接: Mathematician QSC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Jav ...
- 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 ...
- 2016.9.18 --- Shenyang ol
1001 Resident Evil 1002 List wants to travel 1003 hannnnah_j’s Biological Test 1004 Mathematician QS ...
- HDU 5900 QSC and Master 区间DP
QSC and Master Problem Description Every school has some legends, Northeastern University is the s ...
- HDU 5900 QSC and Master (区间DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意:给出序列$A_{i}.key$和$A_{i}.value$,若当前相邻的两个数$A_{ ...
- HDU 5900 - QSC and Master [ DP ]
题意: 给n件物品,有key和value 每次可以把相邻的 GCD(key[i], key[i+1]) != 1 的两件物品,问移除的物品的总value最多是多少 key : 1 3 4 2 移除3 ...
随机推荐
- 一维数组的 K-Means 聚类算法理解
刚看了这个算法,理解如下,放在这里,备忘,如有错误的地方,请指出,谢谢 需要做聚类的数组我们称之为[源数组]需要一个分组个数K变量来标记需要分多少个组,这个数组我们称之为[聚类中心数组]及一个缓存临时 ...
- 关于pydev的语法的错误提示
第三方包引入时,eclipse默认会把一些包定为错误的,错误是:“undefined variable from import...” 其实是对的,可是报错,很烦人 解决方法:window -- pr ...
- C# list distinct操作
使用代理实现对C# list distinct操作 范型在c#编程中经常使用,而经常用list 去存放实体集,因此会设计到对list的各种操作,比较常见的有对list进行排序,查找,比较,去重复. ...
- 由浅入深shell脚本训练
由浅入深shell脚本训练 最近一直在学习Shell,以前一直觉得Shell语法很难,不好学,现在总算有一些收获了.其实Shell脚本就是一堆linux命令的集合,把脚本里每一步的命令搞懂是什么意思, ...
- 安装使用yii-debug-toolbar,yii框架的调试插件
1github 下载yii-debug-toolbar 插件 地址:https://github.com/malyshev/yii-debug-toolbar 2. 假设yii的项目创建在了/expo ...
- C# 根据时间创建文件夹
string file = ((fileNameIndex)index).ToString(); if (!Directory.Exists(HttpContext.Current.Server.Ma ...
- API HOOK库
API HOOK库 API HOOK有两种做法,一种是SetWindowHookEx,简单易用,但如果做其它的HOOK,如HOOK OpenProcess,就需要修改内存地址了,内存地址可以通过Wri ...
- 1.C#基础学习笔记3---C#字符串(转义符和内存存储无关)
技术qq交流群:JavaDream:251572072 教程下载,在线交流:创梦IT社区:www.credream.com ------------------------------------- ...
- 如何制作一个类似Tiny Wings的游戏 Cocos2d-x 2.1.4
在第一篇<如何使用CCRenderTexture创建动态纹理>基础上,增加创建动态山丘,原文<How To Create A Game Like Tiny Wings with Co ...
- Android 经验: 5555 端口会被 adb 误认为 emulator
在本机启动 Android, 再用本机的的 adb 去连接 adb connect 127.0.0.1:5555 而后 adb devices 查看 user@ubuntu:~$ adb device ...