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. UI控件之UINavigationController

    ViewController1 *vc1=[[ViewController1 alloc]init]; UINavigationController *nav1=[[UINavigationContr ...

  2. VC6.0中添加库文件和头文件

    附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools>Options>Directories>Include files. 对于特定项目的头文件包含,在“ ...

  3. Cuckoo Hash——Hash冲突的解决办法

    参考文献: 1.Cuckoo Filter hash算法 2.cuckoo hash 用途: Cuckoo Hash(布谷鸟散列).问了解决哈希冲突的问题而提出,利用较少的计算换取较大的空间.占用空间 ...

  4. token的生成和应用

    token的生成和应用 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是行不通的,因为没有中间用户的授权过程: ...

  5. 使用SQL Server Management Studio 创建数据库备份作业

    --完整备份,每周一次USE MasterGOdeclare @str varchar(100)set @str='D:\Weldon\DBBACK\FullBak'+replace(replace( ...

  6. for update排他锁详解

    使用场景: 高并发并且对于数据的准确性很有要求. 落实到mysql就是在事务中使用,只有使用InnoDB时才用,在begin于commit之间使用(只有此引擎支持事务). 本质: 给表或行上个锁以便接 ...

  7. request模块 一基础部分

    一.HTTP请求   通过requests发送网络请求,方法有get post put delete head options import requests r=requests.get(" ...

  8. 20145240 《Java程序设计》第六周学习总结

    20145240 <Java程序设计>第六周学习总结 教材学习内容总结 InputStream与OutputStream 10.1.1串流设计的概念 Java将输入/输出抽象化为串流,数据 ...

  9. hibernate集合的加载策略

    在集合方配置lazy和fetch的方式,默认的是lazy为true,fetch为select,lazy有true,extra和false,true和extra都是懒加载,只是extra比true更懒, ...

  10. spark学习6(Centos下Scala2.11.4安装)

    Centos下Scala安装 上传Scala到/usr/scala目录下 [root@spark1 scala]# chmod u+x scala-2.11.4.tgz #修改权限 [root@spa ...