题目大意:有$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. 润乾报表整合到Tomcat服务器的部署过程

    转载自:http://www.cnblogs.com/avivaye/archive/2012/11/16/2773681.html 使用第三方的报表设计器/服务器来快速的开发报表. 润乾服务器是使用 ...

  2. linux安装PHP-memcache-redis扩展

    1.php memcache 扩展 http://pecl.php.net/package/memcache/3.0.8 下载文件源码 #tar zxvf memcache-3.0.8.tar#/us ...

  3. Python中print函数中中逗号和加号的区别

    strip()方法,去除字符串开头或者结尾的空格 s = " a b c " new_s = s.strip() print("-------->%s<--- ...

  4. 自己做的一个固定大小对象内存池,效率大概为原始的new/delete的2倍

    提升不高,不过好处是可以多次申请小对象,一次释放.(只适应于无动态申请资源的class) vs2012测试情况如下: // CHchFixLenMemPool.h #pragma once #ifnd ...

  5. 3星|李开复《AI·未来》:中国创业公司有独特优势,人工智能可能会加剧社会的不平等与不稳定

    主要内容:作者对自己一些经历的回顾,对中美两国人工智能行业的回顾与展望. 作者认为中国的创业公司比美国节奏更快工作更拼命,深圳在硬件创新上远远领先于美国,中国创业公司们走出了一条跟美国不同的路. 作者 ...

  6. Oracle同义词和序列

    同义词:是表.索引.视图的模式对象的一个别名,通过模式对象创建同意词,可以隐藏对象的实际名称和 所有者信息,为对象提供一定的安全性,开发应用程序时:应该尽量避免直接使用表,视图 或其他对象,改用对象的 ...

  7. Valgrind 简单用法

    有时需要给自己写的小程序做个简单的 benchmark,查看内存使用情况和运行时间.这时可以试试 valgrind. Ubuntu 下安装很简单: sudo apt-get update sudo a ...

  8. [redis] linux下安装篇(1)

    一.redis是什么redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有 ...

  9. [leetcode-811-Subdomain Visit Count]

    A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...

  10. 欢迎来怼——第四次Scrum会议

    一.小组信息 队名:欢迎来怼小组成员队长:田继平成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华小组照片 二.开会信息 时间:2017/10/16 17:15~17:40,总计25min.地点:东北师范 ...