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 ...
随机推荐
- C#用mouse_event模拟鼠标点击的问题
1.首先添加using System.Runtime.InteropServices; 2.为鼠标添加模拟点击的各种参数 //鼠标事件 因为我用的不多,所以其他参数没有写 1 2 3 4 5 6 7 ...
- Swagger简单介绍
一句话介绍 Swagger Swagger是一个接口文档生成工具,同时提供接口测试调用的辅助功能. 关于 Swagger Swagger能成为最受欢迎的REST APIs文档生成工具之一,有以下几个原 ...
- SpringBoot第十篇:thymeleaf详解
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10931435.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 Sprin ...
- pytorch-04-激活函数
sigmoid函数: 越大的负数越接近0,越大的正数越接近1缺点:(1)造成梯度消失:该函数在靠近1和0的两端,梯度几乎变成0,梯度下降法:梯度乘上学习率来更新参数,如果梯度接近0,那么没有任何信息来 ...
- 前端性能优化--回流(reflow)和重绘(repaint)
HTML加载时发生了什么 在页面加载时,浏览器把获取到的HTML代码解析成1个DOM树,DOM树里包含了所有HTML标签,包括display:none隐藏,还有用JS动态添加的元素等. 浏览器把所有样 ...
- Hbase put写入源码分析
今天有空闲时间看一下HBASE的写入代码 MutiAction类,是一个action的container,包括get . put. delete.并且是根据region name分组的.其中核心的就是 ...
- javascript(六)运算符
运算符概述 JavaScript中的运算符用于算术表达式. 比较表达式. 逻辑表达式. 赋值表达式等.需要注意的是, 大多数运算符都是由标点符号表示的, 比如 "+" 和" ...
- wsl中的git问题
当使用wsl打开Windows下的仓库时可能会出现所有文件都被标记为modified,这时一般有两种情况. 文件权限问题 由于wsl申请对文件的读写权限导致文件的权限发生改变.这时只需修改git的设置 ...
- 将Python源程序打包成可独立执行的文件
有时候需要将编写好的脚本发送给别人,但是在没有安装运行环境或依赖库的情况下,Python脚本程序无法执行.PyInstaller工具可以快速的将python脚本打包成一个二进制可执行的exe程序,并且 ...
- html5新增表单控件和表单属性
表单验证 Invalid事件 : 验证反馈 input.addEventListener('invalid',fn,false) 阻止默认验证:ev.preventDefault() formnova ...