题面

传送门

前置芝士

\(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\),然后代入计算就可以了

于是问题就解决了

  1. //minamoto
  2. #include<bits/stdc++.h>
  3. #define R register
  4. #define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
  5. #define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
  6. #define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
  7. template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
  8. template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
  9. using namespace std;
  10. char buf[1<<21],*p1=buf,*p2=buf;
  11. inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
  12. int read(){
  13. R int res=1,f=1;R char ch;
  14. while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
  15. for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
  16. return res*f;
  17. }
  18. const int N=105,P=998244353,g=3;
  19. inline int add(R int x,R int y,R int P){return x+y>=P?x+y-P:x+y;}
  20. inline int dec(R int x,R int y,R int P){return x-y<0?x-y+P:x-y;}
  21. inline int mul(R int x,R int y,R int P){return 1ll*x*y-1ll*x*y/P*P;}
  22. int ksm(R int x,R int y,R int P){
  23. R int res=1;
  24. for(;y;y>>=1,x=mul(x,x,P))if(y&1)res=mul(res,x,P);
  25. return res;
  26. }
  27. int n,kkk,m;
  28. struct Matrix{
  29. int a[N][N];
  30. Matrix(){memset(a,0,sizeof(a));}
  31. inline int* operator [](const R int &x){return a[x];}
  32. Matrix operator *(Matrix b){
  33. Matrix res;
  34. 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);
  35. return res;
  36. }
  37. }A,B;
  38. Matrix ksm(Matrix x,int y){
  39. Matrix res;fp(i,1,kkk)res[i][i]=1;
  40. for(;y;y>>=1,x=x*x)if(y&1)res=res*x;
  41. return res;
  42. }
  43. map<int,int>mp;
  44. int bsgs(int x){
  45. int m=sqrt(P)+1;mp.clear();
  46. for(R int i=0,res=x;i<m;++i,res=1ll*res*g%P)mp[res]=i;
  47. for(R int i=1,tmp=ksm(g,m,P),res=tmp;i<=m+1;++i,res=1ll*res*tmp%P)
  48. if(mp.count(res))return i*m-mp[res];
  49. }
  50. int exgcd(int a,int b,int &x,int &y){
  51. if(!b)return x=1,y=0,a;
  52. int d=exgcd(b,a%b,y,x);
  53. y-=a/b*x;return d;
  54. }
  55. int a,b,c,d,x,y,t,res;
  56. int main(){
  57. kkk=read();
  58. fp(i,1,kkk)B[i][1]=read();
  59. fp(i,1,kkk-1)B[i][i+1]=1;
  60. A[1][1]=1;
  61. n=read(),m=read();
  62. A=A*ksm(B,n-kkk);
  63. c=bsgs(m),a=A[1][1],b=P-1;
  64. d=exgcd(a,b,x,y);
  65. if(c%d)return puts("-1"),0;
  66. t=abs(b/d);
  67. x=(1ll*x*(c/d)%t+t)%t;
  68. // printf("%d %d\n",x,bsgs(m));
  69. res=ksm(g,x+P-1,P);
  70. printf("%d\n",res);
  71. return 0;
  72. }

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. cf478B-Random Teams 【排列组合】

    http://codeforces.com/problemset/problem/478/B B. Random Teams   n participants of the competition w ...

  2. 高性能Web服务器Nginx的配置与部署研究(9)核心模块之HTTP模块基本常用指令

    一.HTTP模块的作用是什么? Nginx的HTTP模块用于控制Nginx的HTTP进程. 二.指令 1. alias 含义:指定location使用的路径,与root类似,但不改变文件的跟路径,仅适 ...

  3. MAXOS 进程管理

    ps -ef|grep +程序名 注意进程名区分大小写 linux上进程有5种状态:1. 运行(正在运行或在运行队列中等待)2. 中断(休眠中, 受阻, 在等待某个条件的形成或接受到信号)3. 不可中 ...

  4. devcloud 基础架构

           

  5. qmake not exec in ubuntu

    Description Today, I want to run qmake command in terminal, but, it has error message such qmake: co ...

  6. tp5循环+判断

  7. paho_c_pub 使用方法

    Latest Paho Status (2) 摘自:http://modelbasedtesting.co.uk/ I last wrote about the state of Paho in Oc ...

  8. Oracle——约束

    NOT NULLUNIQUE PRIMARY KEYFOREIGN KEYCHECK 如果不指定约束名 ,Oracle server 自动按照 SYS_Cn 的格式指定约束名 --指定约束名 CREA ...

  9. mysql的explain用法

    Mysql—explain的参数详解及用法 EXPLAIN 的每个输出行提供一个表的相关信息,并且每个行包括下面的列: 项 说明 id MySQL Query Optimizer 选定的执行计划中查询 ...

  10. QT学习之常用类的总结

    QApplication 应用程序类 管理图形用户界面应用程序的控制流和主要设置       QPalate   QLabel 标签类 提供文本或者图像的显示   QPushButton 按钮类 提供 ...