The Nth Item

\[Time Limit: 1000 ms \quad Memory Limit: 262144 kB
\]

题意

给出递推式,求解每次 \(F[n]\) 的值,输出所有 \(F[n]\) 的 \(xor\) 值。

思路

对于线性递推数列,可以用特征方程求出他的通项公式,比如这题

\[F[n] = 3F[n-1]+2F[n-2] \\
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{aligned}
&\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 南昌网络赛(递推数列,分段打表)的更多相关文章

  1. 南昌网络赛 H The Nth Item

    南昌网络赛The Nth Item 暴力快速幂+unordered_map记忆化 注意:记忆化不能写到快速幂求解函数里,不断调用函数会造成很大的时间浪费 #include<bits/stdc++ ...

  2. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

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

  4. 线段树+单调栈+前缀和--2019icpc南昌网络赛I

    线段树+单调栈+前缀和--2019icpc南昌网络赛I Alice has a magic array. She suggests that the value of a interval is eq ...

  5. 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)

    题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...

  6. ACM-ICPC 2019南昌网络赛F题 Megumi With String

    ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...

  7. ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval

    ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...

  8. 南昌网络赛C.Angry FFF Party

    南昌网络赛C.Angry FFF Party Describe In ACM labs, there are only few members who have girlfriends. And th ...

  9. 分治维护dp——19南昌网络赛C/cf750E

    南昌网络赛,是cf的原题 第一次做到这种题,所以认真想了下,每次给一个询问[L,R],要求出这个区间里有2017子序列,但是不能有2016子序列需要删掉的最少元素个数 首先如果我们之询问一小段区间[L ...

随机推荐

  1. talk about string,char

    [1].关于sprintf和snprintf的正确使用 考虑以下有缺陷的例子:void f(const char *p){ char buf[11]={0}; sprintf(buf,"%1 ...

  2. java识别死亡或者存活的对象

    那些内存需要回收 内存回收是对运行时内存区域的内存回收,其中程序计数器.虚拟机栈.本地方法栈3个区域随线程而生,随线程而灭:栈中的栈帧随着方法的进入和退出而有条不紊的执行着出栈和入栈操作.每一个栈帧中 ...

  3. linux php composer安装和使用教程

    linux php composer安装和使用教程建议在linux下 下载后 然后再下载到本地               win上最好别用composer下载速度超级慢 或者根本下不动 项目依赖包 ...

  4. SQL --------------- GROUP BY 函数

    Aggregate 函数常常需要添加 GROUP BY 语句,Aggregate函数也就是常说的聚和函数,也叫集合函数 GROUP BY语句通常与集合函数(COUNT,MAX,MIN,SUM,AVG) ...

  5. 02、策略模式(Strategy)

    一.概念: 策略是为达到某一目的而采取的手段或方法,策略模式的本质是目标与手段的分离, 手段不同而最终达成的目标一致.客户只关心目标而不在意具体的实现方法, 实现方法要根据具体的环境因素而变化. 二. ...

  6. thinkphp3.2.3集成phpexcel1.8导出设置单元格合并

    1 到这里下载classes里面的文件 https://github.com/PHPOffice/PHPExcel 2 然后放到 thinkphp的vendor 新建一个文件夹 Phpexcel  然 ...

  7. 性能监控工具的配置及使用 - Spotlight On Oracle(oracle)

    一.    Spotlight On Oracle(oracle)1.1.   工具简介Spotlight是一个强有力的Oracle数据库实时性能诊断工具,提供了一个直观的.可视化的数据库活动展现.S ...

  8. Python——pip快速下载第三方库到指定环境

    pip install Scikit-learn --target=C:/Users/Jery/PycharmProjects/play/venv/Lib/site-packages -i https ...

  9. [转] QML PinchArea

    本文转自安老师的博文:Qt Quick 事件处理之捏拉缩放与旋转 绪论 本文介绍在Android 等智能手机上的一个非常重要的手势:捏拉手势. 捏拉手势最早在苹果手机上得到应用,苹果还曾经尝试为此操作 ...

  10. QT POST/GET HTTP操作

    工程文件 Qt += network 举例 Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui-> ...