https://codeforces.com/contest/1106/problem/F

题意

数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k}_{i-k})\)mod\(P\),给出\(f_{1}...f_{k-1}\)和\(f_{n}\),求\(f_{k}\),其中\(P\)等于998244353

题解

  • 3是998244353的离散对数,所以\(f^{b_1}_{i-1} \equiv 3^{h_i*b_1}(modP)\),怎么求离散对数
  • 乘法转化为加法:\(h_{k+1}\equiv(h_{k}*b_1+...+h_{1}*b_k)mod(P-1)\),矩阵快速幂求出\(h_n\)(\(c*h_k(未知数)\)),\(mod(P-1)\)是欧拉降幂,因为\(h_n\)可能会很大
  • 得到\(f_n=3^{h_n}\equiv m(modP)\),bsgs求出\(h_n\),有空填exbsgs的坑
  • 于是得到\(c*h_k\equiv h_n(modP-1)\)的一元同余方程,用exgcd解出\(h_k\),exgcd还有好多用途
  • 然后\(f_k\equiv3^{h_k}(modP)\)

代码

//vector矩阵快速幂板子
#include<bits/stdc++.h>
#define MOD 998244353
#define ll long long
#define vec vector<ll>
#define mat vector<vec>
using namespace std;
mat mul(mat &A,mat &B,ll mod){
mat C(A.size(),vec(B[0].size()));
for(int i=0;i<A.size();i++)
for(int j=0;j<A.size();j++)
for(int k=0;k<A.size();k++)
C[i][j]+=A[i][k]*B[k][j]%mod,C[i][j]%=mod;
return C;
}
mat pw(mat &A,ll x,ll mod){
mat C(A.size(),vec(A.size()));
for(int i=0;i<A.size();i++)C[i][i]=1;
while(x){
if(x&1)C=mul(C,A,mod);
A=mul(A,A,mod);
x>>=1;
}
return C;
}
ll pw(ll bs,ll x,ll mod){
ll ans=1;
while(x){
if(x&1)ans=ans*bs%mod;
bs=bs*bs%mod;
x>>=1;
}
return ans;
} ll bsgs(ll a,ll b,ll c){
ll m=ceil(sqrt(c));
map<ll,ll>mp;
ll bs=b,BS=pw(a,m,c);
for(int i=1;i<=m;i++){mp[bs]=i-1;bs=bs*a%c;}
bs=1;
for(int i=1;i<=m;i++){
if(mp[bs])return (i-1)*m-mp[bs];
bs=bs*BS%c;
}
return -1;
} ll exgcd(ll a,ll b,ll &x,ll &y){
ll d=a;
if(b==0)x=1,y=0;
else d=exgcd(b,a%b,y,x),y-=x*(a/b);
return d;
}
ll sol(ll a,ll b,ll m){
if(b==0)return 0;
ll g=__gcd(a,m);
if(b%g)return -1;
a/=g;b/=g;m/=g;
ll x,y;
g=exgcd(a,m,x,y);
x=x*b%m;
return (x+m)%m;
}
ll k,n,m,h,ans;
int main(){
cin>>k;
mat A(k,vec(k));
for(int i=0;i<k;i++)cin>>A[i][0];
for(int i=1;i<k;i++)A[i-1][i]=1;
cin>>n>>m;
A=pw(A,n-k,MOD-1);
h=bsgs(3,m,MOD);
ans=sol(A[0][0],h,MOD-1);
if(ans<0)cout<<-1;
else cout<<pw(3,ans,MOD);
}

Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂的更多相关文章

  1. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...

  2. Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

  3. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  4. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  5. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  6. Codeforces Round 536 (Div. 2) (E)

    layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  7. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  8. Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP

    题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...

  9. Codeforces 514E Darth Vader and Tree 矩阵快速幂

    Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define L ...

随机推荐

  1. 关于在centos6 + grub的旧版本中,如何关闭CPU throttling

    由于个人需求,要编译安装ATLAS库,其中就有关闭CPU throttling的步骤, 最常规简单的方法是修改grub /etc/default/grub/ 之后再接一些简单的步骤 + 重启就完成了. ...

  2. base64转码,解码方法

    function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr ...

  3. 爬虫之Selenium 动态渲染页面爬取

    Selenim 是一个自动化测试工具,可以利用它驱动浏览器执行特定的动作,如点击.下拉等操作,同时可以获取浏览器当前呈现的页面的源代码,做到可见及可爬 1.使用流程 1)声明浏览器对象 Seleniu ...

  4. sql优化之concat/concat_ws/group_concat

    原文1:https://baijiahao.baidu.com/s?id=1595349117525189591&wfr=spider&for=pc 原文2:https://www.y ...

  5. step_by_step_ABP规约模式

    一段时间没有在github 上浏览ABP项目,几天前看到ABP新增规约模式,开始了解并学习文档   记录一下 Introduction 介绍 Specification pattern is a pa ...

  6. DVWA中low级的sql注入漏洞的简单复现

    第一次成功复现一个简单漏洞,于是写下这篇随笔记录一下 首先我们来看dvwa中low级的sql注入的源码 源码文件路径如下图: 源码如下: <?php if(isset($_GET['Submit ...

  7. vim matchit 自定义配对关键字之间的跳转

    vim因其强大的扩展性一直深受linux程序员的喜爱,最近在用vim写verilog的时候,由于一个逻辑块中的begin end较多,常常会多写或者漏掉匹配关键字,很是苦恼,于是寻找匹配关键字间跳转的 ...

  8. Mysql数据库左外连接,右外连接,模糊查询

    内连接,左外连接,右外连接都是数据库的常用连接与使用手段 内连接 select * from assets_car c inner join category c on a.id = c.id; 左外 ...

  9. django filter or 多条件查询

    功能:django中实现多条件查询 或关系: from django.db.models import Q return qs.filter(Q(notice_to_group__contains=' ...

  10. 泛型List去除重复指定字段

    泛型List去除重复指定字段ID var list=listTemp.Distinct(new IDComparer ()).ToList(); 重写比较的方法: public class IDCom ...