The Nth Item 南昌网络赛(递推数列,分段打表)
The Nth Item
\]
题意
给出递推式,求解每次 \(F[n]\) 的值,输出所有 \(F[n]\) 的 \(xor\) 值。
思路
对于线性递推数列,可以用特征方程求出他的通项公式,比如这题
x^2 = 3x+2 \\
x = \frac{3\pm \sqrt{17}}{2}
\]
令 \(F[n] = C_1x_1^n + C_2x_2^n\)
将 \(F[0]\) 和 \(F[1]\) 带入
&\begin{cases}
C_1 + C_2 = 0 \\
C_1\frac{3+ \sqrt{17}}{2} + C_2\frac{3- \sqrt{17}}{2} = 1
\end{cases} \\
&\begin{cases}
C_1 = \frac{1}{\sqrt{17}}\\
C_2 = -\frac{1}{\sqrt{17}}
\end{cases}
\end{aligned}
\]
即 \(F[n] = \frac{1}{\sqrt{17}} \left[\left(\frac{3+\sqrt{17}}{2}\right)^n - \left(\frac{3-\sqrt{17}}{2}\right)^n \right]\)
\(\sqrt{17}\) 可以通过二次剩余来得到其中一个可能的解,\(524399943\) 就是一个解。
令 \(p = \frac{3+\sqrt{17}}{2},q=\frac{3-\sqrt{17}}{2}\),现在的问题就是解出 \(p^n\) 和 \(q^n\)。
首先因为 \(n\) 高达 \(1e18\),可以利用欧拉降幂,把 \(n\) 降到 \(2mod-1\) 级别内,也即是 \(2e9\) 附近。
可以利用 \(n = x*50000+y\),\(q^n = q^{x*50000} * q^y\),将 \(q\) 在 \(5e4\) 以内的幂打表出来,在打出 \(q\) 的幂为 \(k*5e4\) 的表,然后就可以做到 \(O(1)\) 查询。
对于 \(q\) 也是相同的做法。
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : Wed 11 Sep 2019 10:01:20 PM CST
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 998244353;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
ll n, m;
int cas, tol, T;
ll fpow(ll a, ll b) {
ll ans = 1;
while(b) {
if(b&1) ans = ans*a%mod;
a = a*a%mod;
b >>= 1;
}
return ans;
}
ll sqrt17 = 524399943, phiC = mod-1;
ll p, q, inv17;
ll ppow1[maxn], ppow2[maxn];
ll qpow1[maxn], qpow2[maxn];
void handle() {
inv17 = fpow(sqrt17, mod-2);
p = 1ll*(3ll+sqrt17)*fpow(2, mod-2)%mod, q = 1ll*(3ll-sqrt17+mod)*fpow(2, mod-2)%mod;
ppow1[0] = qpow1[0] = 1;
for(int i=1; i<=50000; i++) {
ppow1[i] = ppow1[i-1]*p%mod;
qpow1[i] = qpow1[i-1]*q%mod;
}
ppow2[0] = qpow2[0] = 1;
ppow2[1] = ppow1[50000];
qpow2[1] = qpow1[50000];
for(int i=2; i<=50000; i++) {
ppow2[i] = ppow2[i-1]*ppow1[50000]%mod;
qpow2[i] = qpow2[i-1]*qpow1[50000]%mod;
}
}
ll getp(ll n) {
return ppow2[n/50000]*ppow1[n%50000]%mod;
}
ll getq(ll n) {
return qpow2[n/50000]*qpow1[n%50000]%mod;
}
ll solve(ll n) {
if(n >= phiC) n = n%phiC+phiC;
return inv17*(getp(n)-getq(n)+mod)%mod;
}
int main() {
// freopen("in", "r", stdin);
handle();
scanf("%lld%lld", &n, &m);
ll ans = 0;
for(int i=1; i<=n; i++) {
ll tmp = solve(m);
m ^= tmp*tmp;
ans ^= tmp;
}
printf("%lld\n", ans);
return 0;
}
The Nth Item 南昌网络赛(递推数列,分段打表)的更多相关文章
- 南昌网络赛 H The Nth Item
南昌网络赛The Nth Item 暴力快速幂+unordered_map记忆化 注意:记忆化不能写到快速幂求解函数里,不断调用函数会造成很大的时间浪费 #include<bits/stdc++ ...
- 2019 ICPC 南昌网络赛
2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...
- dp--2019南昌网络赛B-Match Stick Game
dp--2019南昌网络赛B-Match Stick Game Xiao Ming recently indulges in match stick game and he thinks he is ...
- 线段树+单调栈+前缀和--2019icpc南昌网络赛I
线段树+单调栈+前缀和--2019icpc南昌网络赛I Alice has a magic array. She suggests that the value of a interval is eq ...
- 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)
题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...
- ACM-ICPC 2019南昌网络赛F题 Megumi With String
ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...
- ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval
ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...
- 南昌网络赛C.Angry FFF Party
南昌网络赛C.Angry FFF Party Describe In ACM labs, there are only few members who have girlfriends. And th ...
- 分治维护dp——19南昌网络赛C/cf750E
南昌网络赛,是cf的原题 第一次做到这种题,所以认真想了下,每次给一个询问[L,R],要求出这个区间里有2017子序列,但是不能有2016子序列需要删掉的最少元素个数 首先如果我们之询问一小段区间[L ...
随机推荐
- 【07月15日】A股滚动市盈率PE最低排名
仅根据最新的市盈率计算公式进行排名,无法对未来的业绩做出预测. 方大集团(SZ000055) - 滚动市盈率PE:2.53 - 滚动市净率PB:1.13 - 滚动年化股息收益率:4.01% - 建筑 ...
- 如何在 VS2015 上开发 Qt 程序
所有Qt版本下载地址: http://download.qt.io/archive/qt/ 所有Qt Creator下载地址: http://download.qt.io/archive/qtcrea ...
- Python2.x升级python3.x【升级步骤和错误总结】
网上帖子一大堆,按照那些教程操作,确实可以成功安装.但是安装成功之后呢,pip还是用的python2的pip. 切换到python3的pip之后,发现无法下载模块,还会有很多报错信息.以及" ...
- PHP 命名空间笔记
PHP 命名空间笔记 1.php文件代码如下<pre><?php//我用这样的命名空间表示处于blog下的article模块namespace Blog\Article; class ...
- 【学习笔记】薛定谔的喵咪Cat—球盒问题(全详解)
[学习笔记]薛定谔的喵咪Cat-球盒问题(全详解) [题目描述] 当一个猫在盒子里时,因为放射物的状态我们不知道,所以猫的状态我们也不知道,这就所谓猫的生死纠缠态,也是所谓的薛定谔的猫. 当我们做需要 ...
- 图解微信小程序---实现行的删除和增加操作
图解微信小程序之实现行的删除和增加操作 代码笔记部分 第一步:在项目的app.json中创建一个新的页面(页面名称英文,可自定义) 第二步:在创建的新页面中编写页面(注意bindtap属性值,因为是我 ...
- 使用Ueditor上传图片到图片服务器(一)
网站的文章,通过运营平台来发布文章(图文消息),上传图片等.百度Ueditor比较成熟就采用了该技术,另外上传的图片是网站系统以及运营平台系统共享的,所以考虑搭建独立的图片服务器,以后也可以提供给公司 ...
- 构建简单Windows Service示例
示例源码:WindowsServiceSample ServiceHelper源码:ServiceHelper 1. 创建Windows Service项目,如图: 2. 配置服务参数 3. 安装,启 ...
- table中td文字超出长度用省略号隐藏超出内容,鼠标点击内容全部显示
1,设置css样式 <style>table {width: 100%;float: left;table-layout:fixed;width:600px;border:1px soli ...
- Jupyter Notebook 打开方法
直接在文件资源管理器的地址栏中输入Jupyter notebook ,即可打开当前目录下的Jupyter.比之前右键打开power shell更方便