跳到正文

题解

全新方法

__YiChaQAQ__o_O约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年9月12日
收录
2026年9月15日
更新
2026年11月11日
篇幅
约 2 分钟
题目
B2064

版本

  1. v1当前

设 fxf_x 为斐波那契数列的第 xx 位。

则 fx=f2×fx−1+f1×fx−2f_x=f_2 \times f_{x-1}+f_1\times f_{x-2}。

再拆一步,则 fx=(f1+f2)×fx−2+f2×fi−3=f3×fx−2+f2×fi−3f_x=(f_1+f_2)\times f_{x-2} + f_2 \times f_{i-3}=f_3\times f_{x-2} + f_2 \times f_{i-3}。

以此类推,fx=(f2+f3)×fx−3+f3×fi−4=f4×fx−3+f3×fi−4f_x=(f_2+f_3)\times f_{x-3} + f_3 \times f_{i-4}=f_4\times f_{x-3} + f_3 \times f_{i-4}。

同理,我们就发现了规律,对于任意 1<v<x1< v <x,fx=fx−v−1×fv+fx−v×fv+1f_x=f_{x-v-1}\times f_v+f_{x-v}\times f_{v+1}。

所以,我们就可以使用递推了。

定义一个数组 aa,如果 axa_x 有值,返回 axa_x,否则向下递推。

可以发现,当 v=⌊x2⌋v=\lfloor \frac x 2 \rfloor 向下递推时,可以将时间复杂度降到很小。

代码如下:

C++
#include<bits/stdc++.h>
using namespace std;
#define R read()
#define int long long
#define mid (l+r>>1)
inline int R{
	int re=0,fg=1;
	char c;
	while (1){
		c=getchar();
		if (('0'<=c and c<='9') or c=='-')	break;
	}
	if (c=='-')	c='0',fg=-1;
	while (1){
		re=re*10+c-'0';
		c=getchar();
		if (!(('0'<=c and c<='9') or c=='-'))	break;
	}
	return re*fg;
}
map<int,int>mp;
int f(int x){
	if (!mp[x])	mp[x]=f(x/2+1)*f(x-x/2)+f(x/2)*f(x-x/2-1);
	return mp[x];
}
signed main(){
	mp[1]=mp[2]=1;//一定要赋值 不然会输出 0
	int T=R;
    while (T--)cout<<f(R)<<"\n"; 
} 
原文