CF1106F Lunar New Year and a Recursive Sequence 原根、矩阵快速幂、BSGS

时间:2023-02-26 22:45:16

传送门


好久没写数论题了写一次调了1h

首先发现递推式是一个乘方的形式,线性递推和矩阵快速幂似乎都做不了,那么是否能够把乘方运算变成加法运算和乘法运算呢?

使用原根!学过\(NTT\)的都知道\(998244353\)的原根\(G=3\)。

使用原根之后,可以得到一个等价的新递推式:\(G^{g_i} = \prod\limits _ {j=1}^k G^{g_{i - j} \times b_j} \mod 998244353(G^{g_i} \equiv f_i\mod 998244353)\),它等价于\(g_i = \sum\limits_{j=1}^k g_{i-j} \times b_j \mod 998244352\)。这样就可以矩阵快速幂得出当\(f_k\)等于某个值\(G^p\)时\(f_n\)的值了。

可现在知道的是\(f_n\)的值,不知道\(f_k\)的值。

考虑:令\(G_k=1\),得到\(G_n\)的值\(x\),那么可以知道\(f_k^x \equiv f_n \mod 998244353\)。

这是一个模意义下的高次剩余方程,要怎么求解呢?

同样使用原根。设\(f_{k} = G^b \mod 998244353\),通过\(BSGS\)求出\(f_n = G^y \mod 998244353\),那么原式变成\(G^{bx} \equiv G^y \mod 998244353\),即\(bx \equiv y \mod 998244352\)。逆元求解方程得到\(b\),也就得到了\(f_k\)。

一些打比赛时被坑到的点:

①\(998244352 = 2^{23} \times 7 \times 17\),求逆元要用欧拉定理或者拓展欧几里得

②\(998244351 \times 998244351 \times 100 > 2^{63}\),这意味着矩阵相乘不能算完再一起取模

#include<bits/stdc++.h>
#define int long long
//This code is written by Itst
using namespace std; inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c)){
if(c == '-')
f = 1;
c = getchar();
}
while(isdigit(c)){
a = (a << 3) + (a << 1) + (c ^ '0');
c = getchar();
}
return f ? -a : a;
} const int MOD = 998244353 , G = 3;
int K;
struct matrix{
int a[100][100];
int* operator [](int x){return a[x];}
matrix(){memset(a , 0 , sizeof(a));}
matrix operator *(matrix b){
matrix c;
for(int i = 0 ; i < K ; ++i)
for(int j = 0 ; j < K ; ++j)
for(int k = 0 ; k < K ; ++k)
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % (MOD - 1);
return c;
}
}S , T; inline int gcd(int a , int b){
int r = a % b;
while(r){
a = b;
b = r;
r = a % b;
}
return b;
} inline int poww(int a , int b , int mod = MOD){
int times = 1;
while(b){
if(b & 1)
times = times * a % mod;
a = a * a % mod;
b >>= 1;
}
return times;
} map < int , int > Hash; inline int BSGS(int x){
int t = sqrt(MOD) + 1 , times = x;
for(int i = 0 ; i < t ; i++){
Hash[times] = i;
times = times * G % MOD;
}
times = poww(G , t);
int now = times;
for(int i = 1 ; i <= t + 1 ; i++){
if(Hash.count(now)){
return i * t - Hash[now];
}
now = now * times % MOD;
}
return -1;
} int phi(int x){
int times = x;
for(int i = 2 ; i * i <= x ; ++i){
if(x % i == 0){
times = times / i * (i - 1);
while(x % i == 0)
x /= i;
}
}
if(x - 1)
times = times / x * (x - 1);
return times;
} signed main(){
#ifndef ONLINE_JUDGE
//freopen("in" , "r" , stdin);
//freopen("out" , "w" , stdout);
#endif
K = read();
for(int i = 0 ; i < K ; ++i)
T[K - i - 1][K - 1] = read() % (MOD - 1);
int N = read() - K;
int t = BSGS(read());
for(int i = 0 ; i + 1 < K ; ++i)
T[i + 1][i] = 1;
S[0][K - 1] = 1;
while(N){
if(N & 1)
S = S * T;
T = T * T;
N >>= 1;
}
int cur = S[0][K - 1] , p = gcd(cur , MOD - 1);
if(t % p != 0)
puts("-1");
else{
t /= p;
cur /= p;
int mod = (MOD - 1) / p;
cout << poww(G , poww(cur , phi(mod) - 1 , mod) * t % mod);
}
return 0;
}

CF1106F Lunar New Year and a Recursive Sequence 原根、矩阵快速幂、BSGS的更多相关文章

  1. CF1106F Lunar New Year and a Recursive Sequence(矩阵快速幂&plus;bsgs&plus;exgcd)

    题面 传送门 前置芝士 \(BSGS\) 什么?你不会\(BSGS\)?百度啊 原根 对于素数\(p\)和自然数\(a\),如果满足\(a^x\equiv 1\pmod{p}\)的最小的\(x\)为\ ...

  2. HDU 5950 Recursive sequence(矩阵快速幂)

    题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...

  3. Recursive sequence (矩阵快速幂)2016ACM&sol;ICPC亚洲区沈阳站

    题目 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recu ...

  4. HDU 5950:Recursive sequence(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...

  5. 【HDU5950】Recursive sequence(矩阵快速幂)

    BUPT2017 wintertraining(15) #6F 题意 \(f(1)=a,f(2)=b,f(i)=2*(f(i-2)+f(i-1)+i^4)\) 给定n,a,b ,\(N,a,b &lt ...

  6. HDU 5950 Recursive sequence(矩阵快速幂)题解

    思路:一开始不会n^4的推导,原来是要找n和n-1的关系,这道题的MOD是long long 的,矩阵具体如下所示 最近自己总是很坑啊,代码都瞎吉坝写,一个long long的输入写成%d一直判我TL ...

  7. 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 ...

  8. A - Number Sequence(矩阵快速幂或者找周期)

    Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...

  9. HDU 5667 Sequence(矩阵快速幂)

    Problem Description Holion August will eat every thing he has found. Now there are many foods,but he ...

随机推荐

  1. 【深入浅出Linux网络编程】 &OpenCurlyDoubleQuote;基础 -- 事件触发机制”

    回顾一下“"开篇 -- 知其然,知其所以然"”中的两段代码,第一段虽然只使用1个线程但却也只能处理一个socket,第二段虽然能处理成百上千个socket但却需要创建同等数量的线程 ...

  2. 日志——JSON的相关方法

    http://www.cnblogs.com/henryxu/archive/2013/03/10/2952738.html JSON  jar包: commons-lang.jar commons- ...

  3. 遍历所有的选中的radio的个数和值

         var ID = "";             $('input:radio:checked').each(function (i) {                 ...

  4. 【谷歌市场安装】Google Play 闪退问题解决

    Google Play 安装后闪退,是因为手机没有内置GMS(Google Mobile Service) 框架. 由于谷歌退出了中国市场,国产手机很多都没有内置GMS, 导致Google Play ...

  5. linux c in common use function reference manual

    End User License Agreement guarantees or warranties,大战前得磨刀!!!!! Tips:C Funcs Chk header Modules!

  6. 解放双手—Cobbler批量自动化部署多版本系统

    1 Cobbler  介绍 Cobbler 是一个 Linux 服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装.重装物理服务器和虚拟机,同时还可以管理 DHCP,DNS 等.Cobble ...

  7. Java 8 中为什么要引出default方法

    (原) default方法是java 8中新引入进的,它充许接口中除了有抽象方法以外,还可以拥用具有实现体的方法,这一点跟jdk8之前的版本已经完全不一样了,为什么要这样做呢? 拿List接口举例,在 ...

  8. 【BZOJ4554】【TJOI2016】【HEOI2016】游戏

    我好弱啊quq 原题: 在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂.简单的说,这个游戏就是在一张地图上放上若干个炸弹,看 是否能炸到对手,或者躲开对手的炸弹.在玩游戏的过程中,小H想到了这样一 ...

  9. Mysql&lowbar;解决The total number of locks exceeds the lock table size错误

    在操作mysql数据库表时出现以下错误. 网上google搜索相关问题,发现一位外国牛人这么解释: If you're running an operation on a large number o ...

  10. C&num; xml通过xslt转换为html输出

    html效果截图: 1.首先分析html代码结构: 结果如图: 2.调用接口返回的数据格式: 3.由第一步可看出每2个数据为一行并排显示,后台返回的数据总数可能为奇数个或偶数个,对应该生成的xml结构 ...