CF1106F Lunar New Year and a Recursive Sequence 原根、矩阵快速幂、BSGS
好久没写数论题了写一次调了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的更多相关文章
- CF1106F Lunar New Year and a Recursive Sequence(矩阵快速幂+bsgs+exgcd)
题面 传送门 前置芝士 \(BSGS\) 什么?你不会\(BSGS\)?百度啊 原根 对于素数\(p\)和自然数\(a\),如果满足\(a^x\equiv 1\pmod{p}\)的最小的\(x\)为\ ...
- HDU 5950 Recursive sequence(矩阵快速幂)
题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...
- Recursive sequence (矩阵快速幂)2016ACM/ICPC亚洲区沈阳站
题目 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recu ...
- 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 ...
- 【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 < ...
- HDU 5950 Recursive sequence(矩阵快速幂)题解
思路:一开始不会n^4的推导,原来是要找n和n-1的关系,这道题的MOD是long long 的,矩阵具体如下所示 最近自己总是很坑啊,代码都瞎吉坝写,一个long long的输入写成%d一直判我TL ...
- 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 ...
- A - Number Sequence(矩阵快速幂或者找周期)
Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * ...
- HDU 5667 Sequence(矩阵快速幂)
Problem Description Holion August will eat every thing he has found. Now there are many foods,but he ...
随机推荐
- 【读书笔记】iOS-发布你的应用
一,添加图标 你的应用在iPhone主屏幕上的标准图标(Icon.png)是57像素*57像素的正方形,PNG格式,不能有透明效果或者图层,72DPI.除些之外,你还可以提供一个同样格式的114像素* ...
- Html/Css 初步认识笔记
1.什么是 HTML ? HTML(HyperText Markup Language) 的学名是超文本标记语言. 标记用来表示网页内容要如何显示,自身不显示 .<我就是标记> 标记成对出 ...
- TFS 安装遇到的问题
居然是是微信桌面客户端占用了8080端口,也是醉了... 1 VS链接 源码管理器 发现提示 Http 404, 发现原来是自己吧tfs 给删除了 2 重新安装tfs,过程中提示 8080 端口被占用 ...
- 使用 ISO镜像配置 本地yum 源(RHEL, CentOS, Fedora等适用)
使用 ISO镜像配置 本地yum 源(RHEL, CentOS, Fedora等适用) 1.上传ISO镜像和挂载 1) 上传Centos7.2 ISO镜像到 /usr/local/src目录 2) ...
- Android事件总线(一)EventBus3.0用法全解析
前言 EventBus是一款针对Android优化的发布/订阅事件总线.简化了应用程序内各组件间.组件与后台线程间的通信.优点是开销小,代码更优雅,以及将发送者和接收者解耦.如果Activity和Ac ...
- 带你熟悉SQLServer2016中的System-Versioned Temporal Table 版本由系统控制的临时表
什么是 System-Versioned Temporal Table? System-Versioned Temporal Table,暂且容我管它叫版本由系统控制的临时表,它是 SQL Serve ...
- percona-toolkit大表操作DDL使用
1. 系统与安装数据库 [root@zhang ~]# cat /etc/redhat-release # 也可以使用其他版本 CentOS Linux release (Core) [root@zh ...
- 第 16 章 C 预处理器和 C 库(string.h 库中的 memcpy() 和 memmove())
/*----------------------------------------- mems.c -- 使用 memcpy() 和 memmove() ---------------------- ...
- 英语初级学习系列-00-Name-介绍自己
1. 询问名字 常用句子 1. Hi, may I have your name, please? 2. Could you please tell me your name? 3. Will it ...
- 【转】ICCAVR TAB键设置
转载于: http://blog.163.com/liuyunqian@yeah/blog/static/7039584320099159545292/ 在使用ICCAVR C编译器的时候会发现TAB ...