http://acm.hdu.edu.cn/showproblem.php?pid=5667

这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t。

发现t是一个递推式,t(n) = c*t(n-1)+t(n-2)+b。这样的话就可以使用矩阵快速幂进行计算了。

设列矩阵[t(n), t(n-1), 1],它可以由[t(n-1), t(n-2), 1]乘上一个3*3的矩阵得到这个矩阵为:{[c, 1, b], [1, 0, 0], [0, 0, 1]},这样指数部分就可以矩阵快速幂了。

但是如果指数不模的话,计算肯定爆了,这里需要考虑费马小定理,a^(p-1) = 1(mod p),于是指数就可以模(p-1)了。

最后算出指数后,再来一次快速幂即可。

但是打这场BC的时候,我并没有考虑到a%p = 0的情况。。。最终错失这题,只过了三题。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#define LL long long using namespace std; //矩阵乘法
//方阵
#define maxN 4
struct Mat
{
LL val[maxN][maxN], p;
int len; Mat()
{
len = ;
} Mat operator=(const Mat& a)
{
len = a.len;
p = a.p;
for (int i = ; i < len; ++i)
for (int j = ; j < len; ++j)
val[i][j] = a.val[i][j];
return *this;
} Mat operator*(const Mat& a)
{
Mat x;
x.p = a.p;
memset(x.val, , sizeof(x.val));
for (int i = ; i < len; ++i)
for (int j = ; j < len; ++j)
for (int k = ; k < len; ++k)
if (val[i][k] && a.val[k][j])
x.val[i][j] = (x.val[i][j] + val[i][k]*a.val[k][j]%p)%p;
return x;
} Mat operator^(const LL& a)
{
LL n = a;
Mat x, p = *this;
memset(x.val, , sizeof(x.val));
x.p = this->p;
for (int i = ; i < len; ++i)
x.val[i][i] = ;
while (n)
{
if (n & )
x = x * p;
p = p * p;
n >>= ;
}
return x;
}
}from, mat; LL n, a, b, c, p; //快速幂m^n
LL quickPow(LL x, LL n)
{
LL a = ;
while (n)
{
a *= n& ? x : ;
a %= p;
n >>= ;
x *= x;
x %= p;
}
return a;
} void work()
{
if (a%p == )
{
if (n == ) printf("1\n");
else printf("0\n");
return;
}
LL t, ans;
if (n == )
t = ;
else if (n == )
t = b%(p-);
else
{
memset(from.val, , sizeof(from.val));
from.val[][] = c;
from.val[][] = ;
from.val[][] = b;
from.val[][] = ;
from.val[][] = ;
from.len = ;
from.p = p-;
mat = from^(n-);
t = (mat.val[][]*b%(p-)+mat.val[][])%(p-);
}
ans = quickPow(a, t);
cout << ans << endl;
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
cin >> n >> a >> b >> c >> p;
work();
}
return ;
}

ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)的更多相关文章

  1. ACM学习历程—HDU5490 Simple Matrix (数学 && 逆元 && 快速幂) (2015合肥网赛07)

    Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progressio ...

  2. Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)

    /* 矩阵乘法+快速幂. 一开始迷之题意.. 这个gcd有个规律. a b b c=a*x+b(x为常数). 然后要使b+c最小的话. 那x就等于1咯. 那么问题转化为求 a b b a+b 就是斐波 ...

  3. 洛谷 P4910 帕秋莉的手环 矩阵乘法+快速幂详解

    矩阵快速幂解法: 这是一个类似斐波那契数列的矩乘快速幂,所以推荐大家先做一下下列题目:(会了,差不多就是多倍经验题了) 注:如果你不会矩阵乘法,可以了解一下P3390的题解 P1939 [模板]矩阵加 ...

  4. 矩阵乘法快速幂 codevs 1250 Fibonacci数列

    codevs 1250 Fibonacci数列  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 定义:f0=f1=1 ...

  5. 矩阵乘法快速幂 codevs 1732 Fibonacci数列 2

    1732 Fibonacci数列 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 在“ ...

  6. ZOJ - 3216:Compositions (DP&矩阵乘法&快速幂)

    We consider problems concerning the number of ways in which a number can be written as a sum. If the ...

  7. 矩阵乘法快速幂 cojs 1717. 数学序列

    矩阵乘法模板: #define N 801 #include<iostream> using namespace std; #include<cstdio> int a[N][ ...

  8. codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数

    对于这道题目以及我的快速幂以及我的一节半晚自习我表示无力吐槽,, 首先矩阵乘法和快速幂没必要太多说吧,,嗯没必要,,我相信没必要,,实在做不出来写两个矩阵手推一下也就能理解矩阵的顺序了,要格外注意一些 ...

  9. [vijos1725&bzoj2875]随机数生成器<矩阵乘法&快速幂&快速乘>

    题目链接:https://vijos.org/p/1725 http://www.lydsy.com/JudgeOnline/problem.php?id=2875 这题是前几年的noi的题,时间比较 ...

随机推荐

  1. FreeRtos堆栈检测应用

    Free rtos每个任务都有自己的栈空间,每个任务需要的栈大小也是不同的.如果堆栈过小就会造成栈溢出,有时候栈溢出发生在某种特定顺序的任务切换中,比较难检测出.所以前期测试和监控任务栈用量就显得尤其 ...

  2. 【Head First Servlets and JSP】笔记11:cookie

    容器如何知道客户是谁?(这并不是HTTP能实现的!IP地址不能唯一的标识用户,另外,非必要不采用HTTPS 继续mark孤傲苍狼的博客,百科全书 cookie——Header——字典——键值对—— 延 ...

  3. Windows定时任务没有执行

    最近部署网站首页静态化程序,需要定时执行的,由于部署在Windows上,为了方便直接用Windows计划任务做定时了.跑了一段时间发现.首页的静态html文件日期一直是老的,手动执行程序会更新,怀疑任 ...

  4. php数组函数-array_intersect()

    array_intersect()函数返回两个或多个数组的交集数组 结果数组包含了所有在被比较数组中,也同时出现在所有其他参数数组中 的值,键名保留不变 array_intersect(array1, ...

  5. ubuntu+vm+ftp

    为了将windows下的文件传到linux中去,使用FZ来做服务器,在linux中进入ftp状态获取. 1.下载FileZilla服务器,在windows下安装就行了(试过汉化插件,用了就报错,所以还 ...

  6. jack server 常见错误解决方法【转】

    本文转载自:https://blog.csdn.net/qq_27061049/article/details/70156200 jack 服务常见错误解决方法 当你编译Android时,你不需要修改 ...

  7. LVS 负载均衡原理详解

    LVS简介 LVS是一个开源软件,由章文嵩博士于1998年5月创立,可以实现Linux平台下的简单负载均衡.LVS是Linux Virtual Server的简写,是一个虚拟的服务器集群系统. LVS ...

  8. Myeclipse快捷键(二)

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+↓ ...

  9. J2EE配置tomcat

  10. 51nod 1040 欧拉函数

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1040 1040 最大公约数之和 题目来源: rihkddd 基准时间限制 ...