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 ...
随机推荐
- SSM(Spring+springMVC+MyBatis)框架-springMVC实现图片上传
关于springMVC来实现图片上传的功能 话不多说,直接上码 1.applicationContext.xml <!-- 配置文件上传 --> <!--200*1024*1024即 ...
- sql语句(已在Oracle中测试,之后有添加内容放在评论中)
1增 1.1[创建一张表] create table 表名(列名 类型); 例: ),性别 ),出生日期 date); ),sex ),出生日期 date); 1.2[插入单行]insert [int ...
- JS数组的基本操作方法
一.concat()concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2,3];var arr2 = [4,5];va ...
- react-redux的使用
在react-redux 框架中,给我提供了两个常用的API来配合Redux框架的使用,其实在我们的实际项目开发中,我们完全可以不用react-redux框架,但是如果使用此框架,就如虎添翼了. 我们 ...
- Java的对象传参问题
在c/c++中对于传参类型,无外乎就是传值.传引用.传指针这几种.但在java中,由于没有指针类型,其传参的方式也发生了相应的变化.之前有搜过相关的知识点一直理解的是:Java的传参方式中主要有两种: ...
- vim 自动添加作者、版权、修改时间等信息
相信大家阅读代码时都见过这样的文件头: # THIS FILE IS PART OF LibreBoot PROJECT (归属) # reboot.py - The core part of the ...
- go语言变量
变量可以通过变量名访问 Go 语言变量名由字母.数字.下划线组成,其中首个字符不能为数字 声明变量的一般形式是使用 var 关键字: var identifier type 变量声明 1. 指定变量类 ...
- snmp监控f5
1.硬盘各分区使用情况 2.pool数量.vs数量 3.cpu使用率 4.内存使用率 5.电源 6.风扇 7.端口状态及流量 8.HA状态(主备情况及HA是否处于建立状态) 9.主备机同步状态
- 古韵之乞巧 题解 dp题
[noip模拟赛1]古韵之乞巧 描述 闺女求天女,更阑意未阑. 玉庭开粉席,罗袖捧金盘. 向月穿针易,临风整线难. 不知谁得巧,明旦试相看. ——祖咏<七夕> 女子乞巧,是七夕的重头戏 ...
- promise之我见
在我们平时的方法中有很多方法是promise封装的, 有些函数后边跟的then和catch 就是promise的方法,先看一下pormise的特点 (1)对象的状态不受外界影响.Promise对象代表 ...