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) ...
随机推荐
- wamp 初始化 修改mysql密码
1.设置phpmyadmin 在WampServer安装完成后,通过http://localhost/打开后可以看到WampServer自带的一个简单的页面,里面有phpinfo.phpmyadmin ...
- css水平居中,竖直居中技巧(二)
css水平居中,竖直居中技巧(二)===### 1.效果 ### 2.代码#### 2.1.index.html <!DOCTYPE html> <html lang="z ...
- Stars URAL - 1028
就是给你一些星星的坐标,然后求出每个星星的左下角有多少颗星星 题目保证按照Y坐标的顺序给出每个星星的坐标,那么我们就可以说,当输入某个星星的坐标时,此时有多少个星星的横坐标小于它,它左下角就有多少星星 ...
- 15-I hate it (HDU1754:线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=1754 相似例题: 敌兵布阵 http://www.cnblogs.com/zhumengdexiao ...
- PythonQt第一例
pythonQt第一例源码如下,主要介绍了简单的使用方式,需要注意的是应用程序的debug版本和release版本必须使用同类型的PythonQt库不可交叉使用. 源码地址:http://files. ...
- code1074 食物链
开3*n的并查集,其中x用来连接与x同类的,x+n用来连接x吃的,x+2*n用来连接x被吃的. 1 x y时,如果 x吃y 或 x被y吃,那么为假话, 否则x与y同类,x吃的y也吃,x被吃的y也被吃: ...
- 洛谷 P3659 [USACO17FEB]Why Did the Cow Cross the Road I G
//神题目(题目一开始就理解错了)... 题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's far ...
- 阿里云OSS-web直传---在服务端c#签名,浏览器直传
OSS web直传---在服务端php签名,浏览器直传 本文:OSS web直传---在服务端c#签名,浏览器直传 其他语言的范例地址:https://help.aliyun.com/document ...
- C/C++语言中的函数参数传参三种对比
学了很长时间C/C++有时指针方面还是有点乱. 希望大神发现如果下面有不对的地方请指出.我发现之所以我乱就是因为中文表述不准确的问题,比如 ,地址值和地址 #include <iostream& ...
- TinyMCE3.x整合教程-Xproer.WordPaster
版权所有 2009-2017 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/wordpa ...