题目大意:有$T(T\leqslant10^5)$组询问,每次求$A_n(n\leqslant10^{18})$:
$$
A_n=\left\lceil\left(\dfrac{\sqrt5+1}2\right)^n\right\rceil
$$
题解:通过打表看题解发现,这个序列是$2,3,5,7,\dots$,即$A_n=A_{n-1}+A_{n-2}-[n\equiv0\pmod2]$,题解中说可以记录三个信息矩阵快速幂一下,然后我并不会处理$[n\equiv0\pmod2]$(果然我最菜)

继续看下去,他说可以构造数列$F$,$F_n=F_{n-1}+F_{n-1}(F_1=1,F_2=3)$,$A_n=F_n+(n\bmod2)$,这样就可以过去了,复杂度$O(2^3T\log_2n)$

但是这样似乎感觉不够优秀,可以把转移矩阵分块预处理出来,$n\leqslant10^{18}<2^{60}$,可以$\sqrt{\sqrt n}$即$2^{15}$分一块,这样就可以在常数复杂度内求出一次的答案了,复杂度$O(4\times2^3T)$

更进一步的是,$F$为斐波那契数列,它在模一个数下有循环节,洛谷上有这么一道题,比如在$998244353$下为$1996488708$,这样就可以取模后分块,就可以只分成两块,减少常数,复杂度$O(2\times2^3T)$(但是我跑的比上一个慢。。。加了编译指令才比上一个快)

卡点:最开始以为矩阵的右下角不会有值

C++ Code:

#include <cstdio>
#include <cctype>
#define N 65537
const int mod = 998244353, cover = (1 << 16) - 1; namespace std {
struct istream {
#define M (1 << 23 | 3)
char buf[M], *ch = buf - 1;
inline istream() { fread(buf, 1, M, stdin); }
inline istream& operator >> (int &x) {
while (isspace(*++ch));
for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
return *this;
}
inline istream& operator >> (long long &x) {
while (isspace(*++ch));
for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
return *this;
}
#undef M
} cin;
struct ostream {
#define M (1 << 22 | 3)
char buf[M], *ch = buf - 1;
inline ostream& operator << (int x) {
if (!x) {*++ch = '0'; return *this;}
static int S[20], *top; top = S;
while (x) {*++top = x % 10 ^ 48; x /= 10;}
for (; top != S; --top) *++ch = *top;
return *this;
}
inline ostream& operator << (const char x) {*++ch = x; return *this;}
inline ~ostream() { fwrite(buf, 1, ch - buf + 1, stdout); }
#undef M
} cout;
} struct Matrix {
int s00, s01, s10, s11;
Matrix() { }
Matrix(int __00, int __01, int __10, int __11) : s00(__00), s01(__01), s10(__10), s11(__11) { }
inline Matrix operator * (const Matrix &rhs) {
#define M(l, r) static_cast<long long> (s##l) * rhs.s##r
#define C(ll, lr, rl, rr) (M(ll, lr) + M(rl, rr)) % mod
return Matrix(C(00, 00, 01, 10), C(00, 10, 01, 11), C(10, 00, 11, 10), C(10, 01, 11, 11));
#undef M
#undef calc
}
} base0[N], base1[N], ans(3, 1, 0, 0); void init() {
const Matrix I(1, 0, 0, 1);
#define work(x) \
*base##x = I; \
for (int i = 1; i < N; ++i) base##x[i] = base##x[i - 1] * __base##x;
const Matrix __base0(1, 1, 1, 0); work(0);
const Matrix __base1 = base0[N - 1]; work(1);
#undef work
} int Tim;
int main() {
init();
std::cin >> Tim;
while (Tim --> 0) {
static long long n;
static int res;
std::cin >> n; --n, n %= 1996488708;
res = (ans * base0[n & cover] * base1[n >> 16 & cover]).s01 + !(n & 1) - mod;
std::cout << res + (res >> 31 & mod) << '\n';
}
return 0;
}

  

[洛谷P5136]sequence的更多相关文章

  1. 洛谷 P3928 Sequence

    题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强.小强便打算找出这三个数列内的最长波动数列. ...

  2. 洛谷 P4597 序列sequence 解题报告

    P4597 序列sequence 题目背景 原题\(\tt{cf13c}\)数据加强版 题目描述 给定一个序列,每次操作可以把某个数\(+1\)或\(-1\).要求把序列变成非降数列.而且要求修改后的 ...

  3. 洛谷UVA12995 Farey Sequence(欧拉函数,线性筛)

    洛谷题目传送门 分数其实就是一个幌子,实际上就是求互质数对的个数(除开一个特例\((1,1)\)).因为保证了\(a<b\),所以我们把要求的东西拆开看,不就是\(\sum_{i=2}^n\ph ...

  4. 洛谷 [USACO17OPEN]Bovine Genomics G奶牛基因组(金) ———— 1道骗人的二分+trie树(其实是差分算法)

    题目 :Bovine Genomics G奶牛基因组 传送门: 洛谷P3667 题目描述 Farmer John owns NN cows with spots and NN cows without ...

  5. 洛谷P1432 倒水问题(CODEVS.1226)

    To 洛谷.1432 倒水问题 题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were co ...

  6. 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]

    题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...

  7. [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns

    洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...

  8. [洛谷P3460] [POI2007]TET-Tetris Attack

    洛谷题目链接:[POI2007]TET-Tetris Attack 题目描述 A puzzle called "Tetris Attack" has lately become a ...

  9. [洛谷P2048] [NOI2010] 超级钢琴

    洛谷题目链接:[NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号 ...

随机推荐

  1. Elasticsearch5.x版本中对Text类型进行聚合时提示illegal_argument_exception

    Having this field in my mapping "answer": { "type": "text", "fiel ...

  2. php实现图形计算器

    存档: index.php <html> <head> <title>图形计算器开发</title> <meta http-equiv=" ...

  3. power sequece

  4. hdu2061 Treasure the new start, freshmen!(暴力简单题)

    Treasure the new start, freshmen! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  5. (转)python+opencv实现动态物体追踪

    原文链接:https://blog.csdn.net/cike14/article/details/50649811 import cv2 import numpy as np camera=cv2. ...

  6. 一学就会pip换镜像源

    首先介绍一个国内好用的镜像站 阿里云 http://mirrors.aliyun.com/pypi/simple/ 豆瓣 http://pypi.douban.com/simple/ 清华大学 htt ...

  7. 该用哪个:Redis与Memcached之间如何选择呢?

    华为云分布式缓存Redis5.0和Memcached都是华为云DCS的核心产品. 那么在不同的使用场景之下,如何选择Redis5.0和Memcached呢? 就由小编为大家进行详细的数据对比分析吧 R ...

  8. 简析Monte Carlo与TD算法的相关问题

    Monte Carlo算法是否能够做到一步更新,即在线学习? 答案显然是不能,如果可以的话,TD算法还有何存在的意义?MC算法必须要等到episode结束后才可以进行值估计的主要原因在于对Return ...

  9. 【算法设计与数据结构】为何程序员喜欢将INF设置为0x3f3f3f3f?(转)

    摘自https://blog.csdn.net/jiange_zh/article/details/50198097 在算法竞赛中,我们常常需要用到一个“无穷大”的值,对于我来说,大多数时间我会根据具 ...

  10. MySort试验记录

    MySort试验记录 编写目标 结果代码 思路 将数组每一个项目的第三组数字抽出来并且排序成12345的顺序,并形成新数组k3. 把原数组的每一项与k3进行比较,每符合一项便输出一项,从而重新排序出新 ...