1. 求斐波那契数列第 项
来源:link
评测:link
设斐波那契数列第 项为 。
对于 ,我们可以注意到:
- 。
- 。
- 。
然后即可发现, 的值中, 的系数为 ,常数项为 。
注意到 。设 ,则有 。
注意到 。设 ,则有 。
对 二进制分解,从第二高位往下计算即可。
#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace Fast{
inline int fr(){
int x = 0,f = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') f = 0;
c = getchar();
}
while (c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return (f ? x : -x);
}
char putstk[1005];
inline void fw(int x){
if (x == 0) return putchar('0'),void();
if (x < 0) x = -x,putchar('-');
int top = 0;
while (x) putstk[++top] = ((x % 10) ^ 48),x /= 10;
while (top) putchar(putstk[top--]);
}
}
using namespace Fast;
const int mod = 998244353;
int a[85];
int calc(int b){
int m = 1,n = 0;
memset(a,0,sizeof(a));
int cnt = 0;
while (b) a[++cnt] = (b & 1),b >>= 1;
for (int i = cnt - 1;i >= 1;i--){
int o = m,p = n;
m = (o * o + 2LL * o * p) % mod;
n = (o * o + p * p) % mod;
if (a[i]){
swap(m,n);
m = (m + n) % mod;
}
}
return m;
}
signed main(){
int n = fr();
if (n <= 2) return fw(1),0;
else fw(calc(n));
return 0;
}