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. html5手机端播放音效不卡的方法

    html5手机端播放音效不卡的方法线下载http://wxserver.knowway.cn/solosea/js/audioEngine.js 这个是性能不错 然后直接播放音效就可以了 audioE ...

  2. HTML+css基础 标签的起名 style标签 选择器的使用规则

    标签的起名: 1. 官方提供的标签名 2. 类名: 用class属性起的名字 3. Id名: 用id属性起的名字 唯一的 我们把这种起名叫选择器 class选择器 id选择器  标签选择器 style ...

  3. 如何定时查询某线程的CPU执行时间

    #!/bin/bash pid=$(ps -T -p $(pgrep xxx) | grep xxx | gawk -F" " '{print $2}') if [ -z $pid ...

  4. for循环居然还可以这样写

    公司代码有点坑,查找问题,发现for循环的写法不是固定条件在中间,写反了也是可以运行的.比如:下面一个简单的for循环 int m=0; for(int i=0;i>3;i++){ m=m+i; ...

  5. logstash之mongodb-log

    1.logstash6.5.3 配置收集mongodb的日志: 首先在mongodb服务器上部署filebeat收集日志并上传到logstash进行处理,然后上传到ES. filebeat-conf: ...

  6. 「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 下)

    大家好,在上一篇系列文章里,我们一起学习了路由的基本配置,如何创建路由和传参,本篇文章我们一起学习下 Navigation 导航和路由守卫的相关内容. Navigation 如果要改变当前路径,我们可 ...

  7. winform IO文件操作

    最近做了一个查错工具,运用了winform文件操作的知识,做了几点总结,不全面,只总结了几点项目里用过的知识(关于以下内容只是个人的理解和总结,不对的地方请多指教,有补充的可以评论留言大家一起讨论学习 ...

  8. Python - 注释 - 第四天

    注释 确保对模块, 函数, 方法和行内注释使用正确的风格 Python中的注释有单行注释和多行注释: Python中单行注释以 # 开头,例如: # 这是一个注释 print('Hello Pytho ...

  9. DjangoDRF总结

    思维导图xmind文件:https://files-cdn.cnblogs.com/files/benjieming/DRF%E6%A8%A1%E5%9D%97.zip

  10. 深入浅出JVM之垃圾收集算法

    判断哪些对象需要被回收 引用计数算法: 给对象中添加一个引用计数器,每当有一个地方引用时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象就是不可能再被使用的. 但是JVM没有使 ...