HDU 5667 Sequence 矩阵快速幂
官方题解:
观察递推式我们可以发现,所有的fi都是a的幂次,所以我们可以对fi取一个以a为底的log,gi=loga fi
那么递推式变gi=b+c∗gi−1+gi−2,这个式子可以矩阵乘法
这题有一个小trick,注意a mod p=0的情况.
分析:排除了a mod p=0的情况,幂次可以对(p-1)取模,这是由于离散对数定理
相关定理请查阅 算导
吐槽:比赛的时候就是被a mod p=0这种情况给hack掉了,我太弱了
#include <stdio.h>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=1e5+;
LL p,a,b,c,n;
struct asd{
LL c[][];
};
asd mul(asd a,asd b){
asd d;
for(int i=;i<=;++i){
for(int j=;j<=;++j){
d.c[i][j]=;
for(int k=;k<=;++k)
d.c[i][j]=(d.c[i][j]+a.c[i][k]*b.c[k][j]%(p-))%(p-);
}
}
return d;
}
asd fun(LL m){
asd a,e;
for(int i=;i<=;++i)
for(int j=;j<=;++j)
a.c[i][j]=e.c[i][j]=;
a.c[][]=c;
a.c[][]=;
a.c[][]=b;
a.c[][]=;
a.c[][]=;
e.c[][]=e.c[][]=e.c[][]=;
while(m){
if(m&)e=mul(e,a);
m>>=;
a=mul(a,a);
}
return e;
}
LL fun2(LL a,LL x){
LL res=;
while(x){
if(x&)res=(res*a)%p;
x>>=;
a=(a*a)%p;
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%I64d%I64d%I64d%I64d%I64d",&n,&a,&b,&c,&p);
if(n==){
printf("1\n");
continue;
}
if(n==){
printf("%I64d\n",fun2(a,b));
continue;
}
if(a%p==){
printf("0\n");
continue;
}
asd t=fun(n-);
LL x=;
x=(x+t.c[][]*b%(p-))%(p-);
x=(x+t.c[][])%(p-);
printf("%I64d\n",fun2(a,x));
}
return ;
}
HDU 5667 Sequence 矩阵快速幂的更多相关文章
- HDU 5667 Sequence 矩阵快速幂+费马小定理
题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- E - Recursive sequence HDU - 5950 (矩阵快速幂)
题目链接:https://vjudge.net/problem/HDU-5950 思路: 构造矩阵,然后利用矩阵快速幂. 1 #include <bits/stdc++.h> 2 #inc ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- 布局时margin会影响父元素
布局时margin会影响父元素.md 在布局使用margin时 <div class="login-bg"> <div class="login&quo ...
- 51nod动态规划-----矩阵取数
一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下向右走,求能够获得的最大价值. 例如:3 * 3的方格. 1 3 3 2 1 3 2 2 1 能够获得的最 ...
- MySQL中SQL语句的分类
1:数据定义语言(DDL) :创建和删除数据库(CREATE DATABASE || DROP DATABASE):2:创建.修改.重命名.删除表(CREATE TABLE || ALTER TA ...
- php之面向对象(1)
讲到面向对象 先回顾下以前的编程思路,所谓编程思路就是根据知识本质原理通过逻辑推理程序的过程,编程思路,讲究的是先明确要做的事情是怎么.离开代码的情况下,自己也要能明白这一件事情怎么做.而不是把代码背 ...
- hdu 5429 Geometric Progression 高精度浮点数(java版本)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5429 题意:给一段长度不超过100的每个数字(可以是浮点数)的长度不超过1000的序列,问这个序列是否 ...
- hdu 5172 GTY's gay friends
GTY's gay friends 题意:给n个数和m次查询:(1<n,m<1000,000);之后输入n个数值(1 <= ai <= n):问下面m次查询[L,R]中是否存在 ...
- STL的pair学习, map学习
http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...
- hadoop 补充(转)
1.输入文件: 文件是MapReduce任务的数据的初始存储地.正常情况下,输入文件一般是存在HDFS里.这些文件的格式可以是任意的:我们可以使用基于行的日志文件,也可以使用二进制格式,多行输入记录或 ...
- 9.MVC框架开发(关于ActionResult的处理)
1.在Action处理之后,必须有一个返回值,这个返回值必须继承自ActionResult的对象 2.ActionResult,实际就是服务器端响应给客户端的结果 ViewResult(返回视图结果) ...
- ElasticSearch入门-搜索如此简单
搜索引擎我也不是很熟悉,但是数据库还是比较了解.可以把搜索理解为数据库的like功能的替代品.因为like有以下几点不足: 第一.like的效率不行,在使用like时,一般都用不到索引,除非使用前缀匹 ...