传送门


好久没写数论题了写一次调了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(矩阵快速幂+bsgs+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/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 < ...

  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. 开发Hexo主题(一)

    Hexo是一款静态博客工具,由Node.js编写 在博客根目录的themes下,新建文件夹(文件夹名为主题名) 主题目录结构如图 这里使用模版引擎为Ejs 在Layout目录下新建index.ejs文 ...

  2. 纯小白入手 vue3.0 CLI - 3.2 - 路由的初级使用

    vue3.0 CLI 真小白一步一步入手全教程系列:https://www.cnblogs.com/ndos/category/1295752.html 尽量把纷繁的知识,肢解重组成为可以堆砌的知识. ...

  3. Intel P4 CPU

    1. P4 CPU 结构 奔4处理器是Intel的经典之作,它是采用乱序执行内核的超标量处理器.P4采用的微架构称为 Net Burst,基本结构如下: 奔4处理器微架构被分成了4大部分: (1)存储 ...

  4. python中remove的一些坑

    前几天,使用python时遇到这么一个需求,删除一个列表中值为1的元素.我寻思着使用remove方法,但是remove方法只会删除第一个,于是我使用for循环去删除.代码和运行结果如下: 当时这个结果 ...

  5. 安装slide后Powerpoint 不自动退出的解决方案

    症状 打开PPT文件,powerpoint界面不启动. 原因 安装slide之后,powerpoint关闭后,powerpnt.exe进程不正常退出,需要手工终止. 解决方案 打开cmd,进入slid ...

  6. 在Centos7下搭建Git服务器

    ① 安装 Git ② 服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码 ③ 服务器端创建 Git 仓库 ④ 客户端 clone 远程仓库 ⑤ 客户端创建 SSH 公钥和私 ...

  7. RD340服务器安装windows2003系统

    RD340服务器安装windows2003系统云修网

  8. python windows环境下文档备份

    #python 2.7 #Filename:backup.py import os import time source = [r'C:\Users\zeng.shufang\Desktop\mess ...

  9. python第五十课——多态性

    animal.py class Animal: def __init__(self,name): self.name = name def eat(self): pass dog.py from an ...

  10. attachBaseContext

    at android.content.ContextWrapper.attachBaseContext(ContextWrapper.java:66) at android.view.ContextT ...