CF1106F Lunar New Year and a Recursive Sequence(矩阵快速幂+bsgs+exgcd)
题面
前置芝士
\(BSGS\)
什么?你不会\(BSGS\)?百度啊
原根
对于素数\(p\)和自然数\(a\),如果满足\(a^x\equiv 1\pmod{p}\)的最小的\(x\)为\(p-1\),那么\(a\)就是\(p\)的一个原根
离散对数
对于素数\(p\),以及\(p\)的一个原根\(g\),定义\(y\)为\(x\)的离散对数,当且仅当\(g^y\equiv x\pmod{p}\),记\(y\)为\(ind_g x\)。不难发现原数和离散对数可以一一对应。也不难发现离散对数用\(bsgs\)就可以求得
题解
考虑把题目转化一下,因为\(f_{1,...,k-1}\)都是\(1\),只有\(f_k\)不是\(1\),那么最终的\(f_n\)一定是形如\({f_k}^x\)的形式
那么我们只考虑上面的次数的转移,转移式就可以从一个前面一堆乘起来变成前面一堆加起来的形式。矩阵快速幂求出最终的\(f_n\)中的次数就行了
那么就是一个\({f_k}^x\equiv f_n\pmod{p}\)的形式了,其中\(x\)我们之前已经用矩阵快速幂算出来了
于是就是关于形如\(x^a\equiv b\pmod{p}\)形式的方程求解的问题了
考虑两边取离散对数,学过\(NTT\)的都知道\(998244353\)的原根是\(3\),那么就可以转化成\(a\times ind_g x\equiv ind_g b\pmod{p-1}\),用\(exgcd\)解出\(ind_gx\),然后代入计算就可以了
于是问题就解决了
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res=1,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
const int N=105,P=998244353,g=3;
inline int add(R int x,R int y,R int P){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y,R int P){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y,R int P){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y,R int P){
R int res=1;
for(;y;y>>=1,x=mul(x,x,P))if(y&1)res=mul(res,x,P);
return res;
}
int n,kkk,m;
struct Matrix{
int a[N][N];
Matrix(){memset(a,0,sizeof(a));}
inline int* operator [](const R int &x){return a[x];}
Matrix operator *(Matrix b){
Matrix res;
fp(i,1,kkk)fp(k,1,kkk)fp(j,1,kkk)res[i][j]=add(res[i][j],mul(a[i][k],b[k][j],P-1),P-1);
return res;
}
}A,B;
Matrix ksm(Matrix x,int y){
Matrix res;fp(i,1,kkk)res[i][i]=1;
for(;y;y>>=1,x=x*x)if(y&1)res=res*x;
return res;
}
map<int,int>mp;
int bsgs(int x){
int m=sqrt(P)+1;mp.clear();
for(R int i=0,res=x;i<m;++i,res=1ll*res*g%P)mp[res]=i;
for(R int i=1,tmp=ksm(g,m,P),res=tmp;i<=m+1;++i,res=1ll*res*tmp%P)
if(mp.count(res))return i*m-mp[res];
}
int exgcd(int a,int b,int &x,int &y){
if(!b)return x=1,y=0,a;
int d=exgcd(b,a%b,y,x);
y-=a/b*x;return d;
}
int a,b,c,d,x,y,t,res;
int main(){
kkk=read();
fp(i,1,kkk)B[i][1]=read();
fp(i,1,kkk-1)B[i][i+1]=1;
A[1][1]=1;
n=read(),m=read();
A=A*ksm(B,n-kkk);
c=bsgs(m),a=A[1][1],b=P-1;
d=exgcd(a,b,x,y);
if(c%d)return puts("-1"),0;
t=abs(b/d);
x=(1ll*x*(c/d)%t+t)%t;
// printf("%d %d\n",x,bsgs(m));
res=ksm(g,x+P-1,P);
printf("%d\n",res);
return 0;
}
CF1106F Lunar New Year and a Recursive Sequence(矩阵快速幂+bsgs+exgcd)的更多相关文章
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- 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 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU5950 Recursive sequence (矩阵快速幂)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- CF1106F Lunar New Year and a Recursive Sequence
题目链接:CF1106F Lunar New Year and a Recursive Sequence 大意:已知\(f_1,f_2,\cdots,f_{k-1}\)和\(b_1,b_2,\cdot ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- App审核被拒(后台定位被拒,ipv6被拒,广告标示被拒的解决方案)
ipv6被拒问题描述: 解决方案支持ipv6 1)搭建ipv6 环境,搭建好的ipv6 ,环境会有一个共享wifi, 具体如何搭建ipv6测试环境参考本地如何搭建IPv6环境测试你的APP2)app连 ...
- 112. Path Sum二叉树路径和
[抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- linux之shell编程初步
#################适用于CentOS6################## #!/bin/bash ########################################## ...
- Loadrunner11无法在win7 64位上启用ie解决办法
Loadrunner11无法在win7 64位上启用ie解决办法 1.loadrunner11在win7 64位上默认启用的是32位的那个IE浏览器,路径:C:\Program Files (x86) ...
- jedis的publish/subscribe[转]含有redis源码解析
首先使用redis客户端来进行publish与subscribe的功能是否能够正常运行. 打开redis服务器 [root@localhost ~]# redis-server /opt/redis- ...
- Red Hat 6.5 Samba服务器的搭建(匿名访问,免登录)
搭建Samba服务器是为了实现Linux共享目录之后,在Windows可以直接访问该共享目录. 现在介绍如何在红帽6.5系统中搭建Samba服务. 搭建Samba服务之前,yum源必须配置好,本地源和 ...
- [GO]文件的读写
首先写一个文件 package main import ( "os" "fmt" ) func WriteFile(path string) { //打开文件, ...
- yii2项目实战-路由美化以及如何正确的生成链接
yii2项目实战-路由美化以及如何正确的生成链接 更新于 2016年12月17日 by 白狼 被浏览了 705 次 美化路由 何为美化路由呢?美化嘛,无外乎就是给路由化化妆,让她好看点.我虽没化过妆, ...
- C# GDI+绘图介绍
最近查阅网上资料,将GDI+的基本知识汇总如下: 一.基本的知识 GDI+:Graphics Device Interface Plus也就是图形设备接口,提供了各种丰富的图形图像处理功能; 在C#. ...
- Spring框架总结(十一)
切入点表达式 可以对指定的“方法”进行拦截:从而给指定的方法所在的类生层代理对象. 其他跟十一样,只更改bean.xml <?xml version="1.0" encodi ...