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. extern "C" 有关问题

    之前帮老板编译一个库的代码,遇到了一些问题,后来发现问题出现在extern "C"语法上. 1. C/C++语法extern 关键字 extern是C/C++语言中表明函数和全局变 ...

  2. Python学习进程(7)字符串

        本节介绍字符串的创建与操作方法.     (1)创建字符串:     创建字符串既可以用单引号也可以用双引号: root@SJM:/home/sunjimeng/桌面# cat text.py ...

  3. pdoModel封装

    <?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/7/24 * Time: 14:03 */ /** * 数 ...

  4. @MarkFan 口语练习录音 20140406 [美女与野兽的口语练习录音]

    大家好,您现在收听的是美女与野兽的口语练习录音 敢于追求,不惧任何挑战,才是勇敢的人生.试想一下,世界上每天有多少人为了梦想,为了生活,甚至是为了别人在不停地奔跑.假若你此刻心中装有梦想,却碍于现实不 ...

  5. Raspberry 2B && Ubuntu mate 16.04 && *** 完美透明代理

    Raspberry 2B && Ubuntu mate 16.04 && *** 完美透明代理 关键词:Raspberry 2B, Ubuntu mate 16.04 ...

  6. nodejs/REPL环境命令行操作命令

    1,输入node 进入node[REPL]环境 2,按两次[ctrl+c]退出node[REPL]环境 3,上箭头会查找上次输入的命令 4,cls清屏 5,tab键会自动补全路径 6,REPL环境

  7. linux+udp+server+client

    一.客户端 #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include ...

  8. Qt使用QCustomPlot开发

    一.入门 1.下载源文件http://www.qcustomplot.com/: 2.把.cpp和.h放在工程目录下,并将cpp和h加入工程: 3.在.pro中:QT += printsupport: ...

  9. Kubernetes List-Watch

    list-watch,作为k8s系统中统一的异步消息传递方式,对系统的性能.数据一致性起到关键性的作用. list-watch操作需要做这么几件事: 由组件向apiserver而不是etcd发起wat ...

  10. vc 判断当前用户是否在管理员组以及是否SYSTEM权限运行

    BOOL IsUserInAdminGroup() //判断是否在管理员组 { BOOL fInAdminGroup = FALSE; HANDLE hToken = NULL; HANDLE hTo ...