[洛谷P5136]sequence
题目大意:有$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的更多相关文章
- 洛谷 P3928 Sequence
题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强.小强便打算找出这三个数列内的最长波动数列. ...
- 洛谷 P4597 序列sequence 解题报告
P4597 序列sequence 题目背景 原题\(\tt{cf13c}\)数据加强版 题目描述 给定一个序列,每次操作可以把某个数\(+1\)或\(-1\).要求把序列变成非降数列.而且要求修改后的 ...
- 洛谷UVA12995 Farey Sequence(欧拉函数,线性筛)
洛谷题目传送门 分数其实就是一个幌子,实际上就是求互质数对的个数(除开一个特例\((1,1)\)).因为保证了\(a<b\),所以我们把要求的东西拆开看,不就是\(\sum_{i=2}^n\ph ...
- 洛谷 [USACO17OPEN]Bovine Genomics G奶牛基因组(金) ———— 1道骗人的二分+trie树(其实是差分算法)
题目 :Bovine Genomics G奶牛基因组 传送门: 洛谷P3667 题目描述 Farmer John owns NN cows with spots and NN cows without ...
- 洛谷P1432 倒水问题(CODEVS.1226)
To 洛谷.1432 倒水问题 题目背景 In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were co ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]
题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...
- [洛谷P2852] [USACO06DEC]牛奶模式Milk Patterns
洛谷题目链接:[USACO06DEC]牛奶模式Milk Patterns 题目描述 Farmer John has noticed that the quality of milk given by ...
- [洛谷P3460] [POI2007]TET-Tetris Attack
洛谷题目链接:[POI2007]TET-Tetris Attack 题目描述 A puzzle called "Tetris Attack" has lately become a ...
- [洛谷P2048] [NOI2010] 超级钢琴
洛谷题目链接:[NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号 ...
随机推荐
- 「Leetcode」975. Odd Even Jump(Java)
分析 注意到跳跃的方向是一致的,所以我们需要维护一个数接下来跳到哪里去的问题.换句话说,就是对于一个数\(A_i\),比它大的最小值\(A_j\)是谁?或者反过来. 这里有两种方案,一种是单调栈,简单 ...
- v-model 双向数据绑定
通过v-model指令可以实现双向数据绑定 HTML部分: <div id="app"> <input type="text" v-model ...
- springboot集成jpa,在postgresql数据库中创建主键自增表
依赖文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:/ ...
- centos7.2 apache开启.htaccess
打开httpd.conf(在那里? APACHE目录的CONF目录里面),用文本编纂器打开后,查找 (1) AllowOverride None 改为 AllowOverride All (2)去掉下 ...
- visual studio 2010 和 VSS(Visual SourceSafe)的连接使用
visual studio 2010 和 VSS(Visual SourceSafe)的连接使用 1. 在visual vstudio中选择使用VSS插件: 2. 使用VSS进行源码管理: ...
- 扩展Lucas定理 扩展Lucas板子
题意概述:多组询问,给出N,K,M,要求回答C(N,K)%M,1<=N<=10^18,1<=K<=N,2<=M<=10^6 分析: 模数不为质数只能用扩展Lucas ...
- scrum立会报告+燃尽图(第二周第七次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2252 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公冶 ...
- 第六周的PSP
本周PSP: 本周进度条: 累积进度图:: 本周PSP饼状图:
- Improving the Safety, Scalability, and Efficiency of Network Function State Transfers
Improving the Safety, Scalability, and Efficiency of Network Function State Transfers 来源:ACM SIGCOMM ...
- 团队Alpha冲刺(二)
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...