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) ...
随机推荐
- PHP知识分享之session && cookie
<?php /* * @Content : session && cookie * @author : Mr.xml * @time : 2015-3-17 09:53:49 * ...
- tomcat错误信息解决方案 严重:StandardServer.await:
看到这个报错我的第一反应就是端口被占用,用netstat -ant命令查看发现8080端口没有被占用,也可以看到 tomcat的进程已经存在,但是不能对外提供服务. 1.独立运行的tomcat.exe ...
- Struts2 Annotation 默认返回Tiles2布局
Struts2的annotation方式很简约,特别实在遵从默认约定的时候就根本不需要配什么struts.xml.网上关于Annotation约定大于配置的教程也很多,其中也不乏将xml版struts ...
- C#基础(四)——ref与out的区别
1.ref传进去的参数必须进行初始化,out不必int i;SomeMethod( ref i );//语法错误SomeMethod( out i );//通过 2.ref传进去的参数在函数内部可以直 ...
- iOS极光推送集成步骤
1.下载SDK,导入Xcode 2.在苹果开发者中心建立AppId与bundleID进行关联,注意勾选推送功能 3.在苹果开发者中心建立推送证书 4.在极光后台建立应用且上传推送证书 5.建立描述文件 ...
- js:合同-已知起始日期、年限,自动计算截止日期
dateAddYear('2016-01-01', '3') ;//返回:2018-12-31 浏览器:ie11,ff 46.0.1(成功)360v8.1(急速模式,成功) 浏览器:360v8.1(兼 ...
- call_user_func_array
call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 参数 callback 被调用的回调函数. param_arr 要被传入回调函数的数组,这个数组得是索引 ...
- RSA使用 常识
1公钥加密,私钥解密 OK反过来, 私钥加密,公钥解密 也OK 2 使用RSA加密 对称算法的key ,用对称算法加密 消息.伙伴收到消息后,RSA解密出 对称算法的key,再用这个key去解密消息 ...
- Python数据库连接池实例——PooledDB
不用连接池的MySQL连接方法 import MySQLdbconn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='my ...
- 如何做到 jQuery-free?
一.选取DOM元素 jQuery的核心是通过各种选择器,选中DOM元素,可以用querySelectorAll方法模拟这个功能. var $ = document.querySelectorAll.b ...