题面

传送门

前置芝士

\(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)的更多相关文章

  1. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

  2. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  3. HDU5950 Recursive sequence —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others)   ...

  4. 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 ...

  5. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. HDU5950 Recursive sequence (矩阵快速幂)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. 5950 Recursive sequence (矩阵快速幂)

    题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...

  8. 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 ...

  9. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. MongoDB动态条件之分页查询

    一.使用QueryByExampleExecutor 1. 继承MongoRepository public interface StudentRepository extends MongoRepo ...

  2. 在SharePoint解决方案中使用JavaScript (1) – 引用.js文件

    本文是系列文章的第一篇. 在SharePoint解决方案中使用JavaScript (0) 作为在SharePoint应用程序中使用JavaScript的第一步,就是要知道如何将一个写好的.js文件, ...

  3. Nginx通过CORS实现跨域

    Nginx通过CORS实现跨域 2016-09-01 10:33 阅读 9.4k 评论 0 社区广播:运维派(Yunweipai.com)是国内最早成立的IT运维社区,欢迎大家投稿,让运维人不再孤寂的 ...

  4. golang怎么使用redis,最基础的有效的方法

    最近在学GO语言,我自己也喜欢使用redis,于是乎就顺便把go操作redis的方法也给学了,有个第三方包,在GitHub上面找的 go get github.com/alphazero/Go-Red ...

  5. Apache CloudStack Features

    As a mature and turnkey Infrastructure-as-a-Service (IaaS) platform, Apache CloudStack has a compreh ...

  6. 解决VirtualBox 上的XP 关机时重启 , 启动时蓝屏 ,点击电源选项蓝屏

    三个问题一次性解决. 启动时的蓝屏显示错误信息是: STOP 0x000000CE (...) DRIVER_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATION ...

  7. Openssl ecparam命令

    一.简介 椭圆曲线密钥参数生成及操作 二.语法 openssl ecparam [-inform DER|PEM] [-outform DER|PEM] [-in filename] [-out fi ...

  8. chrome crx下载路径

    chrome crx下载后会被删除,可在检查时粘贴出来,下载路径在: %localappdata%\Google\Chrome\User Data\Webstore Downloads 参考:http ...

  9. data cleaning

    Cleaning data in Python   Table of Contents Set up environments Data analysis packages in Python Cle ...

  10. 【转】C语言中DEFINE简介及多行宏定义

    要写好C语言,漂亮的宏定义是非常重要的.宏定义可以帮助我们防止出错,提高代码的可移植性和可读性等. 在软件开发过程中,经常有一些常用或者通用的功能或者代码段,这些功能既可以写成函数,也可以封装成为宏定 ...