Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
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(新坑) + 欧拉降幂的更多相关文章
- 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} \] 思路 我们通过迭代发 ...
- Codeforces Round #307 (Div. 2) D 矩阵快速幂+快速幂
D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round 536 (Div. 2) (E)
layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP
题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...
- Codeforces 514E Darth Vader and Tree 矩阵快速幂
Darth Vader and Tree 感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦. #include<bits/stdc++.h> #define L ...
随机推荐
- Sping4之依赖注入
Spring的依赖注入可以是我们不需要去管理bean,网上看到一个回答很精辟: 现在你需要买一把锤子: 1.自己做一把,解释成java就是,调用者创建被调用着,也就是自己去创造一个造锤子的方法,然后自 ...
- EXPRESS项目PM2启动NODE_ENV传参数不生效问题解决方法
expree项目开发完,涉及到不同环境,要在启动到时候就要配置好环境变量, packge.json文件如下: "scripts": { "dev": " ...
- SourceTree提交不了,报git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags origin master:master
刚下载好的Soucetree,拉好项目代码却提交不了,害的我百度了好一小会,下面我把我自己最终的解决方案介绍给大家,希望对你们有用. 首先打开 下载好的git 输入命令 ssh-keygen -t ...
- failed to find global analyzer [uax_url_email]
ES的默认分词设置是standard,这个在中文分词时就比较尴尬了,会单字拆分,比如我搜索关键词“清华大学”,这时候会按“清”,“华”,“大”,“学”去分词,然后搜出来的都是些“清清的河水”,“中华儿 ...
- kafka创建会话,报Error while executing topic command : Replication factor: 1 larger than available brokers: 0.
bin/kafka-topics.sh --create --zookeeper es1:2181 --replication-factor 1 --partitions 1 --topic top ...
- kettle使用笔记1--基本安装和使用
参考来源 https://blog.csdn.net/qq_36698956/article/details/80751655,在这个文章基础上实际使用增加的. 一,安装,采用的是下载官方网站的win ...
- PhoenixFD插件流体模拟——UI布局【Interaction】详解
流体交互 本文主要讲解Interaction折叠栏中的内容.原文地址:https://docs.chaosgroup.com/display/PHX3MAX/Liquid+Interaction 主要 ...
- 【Android端】【日志收集上报SDK相关内容测试的方案梳理总结】
测试方案: 主要从几个方面关注,功能 性能 服务端策略(目前所有的这些上报收集等都会通过开关的精细化,通过接口方式将信息返回给APP端,APP端根据相关内容进行上报,因此基于此的上报机制及收集机制都需 ...
- CentOS7+CDH5.14.0安装CDH错误排查:HBase服务出现 该运行状况测试不良,因为 Service Monitor 未找到活动 Master
错误:HBase服务出现 该运行状况测试不良,因为 Service Monitor 未找到活动 Master 如果重启服务之后无法排除该问题,请执行如下操作(CM换成自己的版本号): rm -f /o ...
- Analysis Services(SSAS) 性能优化
1.聚合选项中添加聚合,以空间换时间提升性能. 如下图: 性能提升百分比越高,聚合数越高,生成的Cube越大,这就是以空间换时间. 2.修改SSAS服务器上的线程池配置选项.提升并发数. 通过X:\P ...