设 为斐波那契数列的第 位。
则 。
再拆一步,则 。
以此类推,。
同理,我们就发现了规律,对于任意 ,。
所以,我们就可以使用递推了。
定义一个数组 ,如果 有值,返回 ,否则向下递推。
可以发现,当 向下递推时,可以将时间复杂度降到很小。
代码如下:
#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";
}