题目大意

\(t\le 10^5\)组询问

每次询问\(1\le n\le 10^{18}\)

求有多少种\(n\)的\(fibonacci\)分解

分解定义为:每个\(fib\)数最多选一个,且和为\(n\)

分解出来的数是无序的

分析

妙不可言

可以先将\(n\)分解无相邻\(1\)的\(fib\)码

对于每个\(1\),可以拆分成\(1\),\(011\),\(01011\),\(0101011\),\(\cdots\)(最高位对齐)

然后再通过\(dp\)求解答案

单词复杂度为\(O(\log)\)

做法

先生成前\(90\)个\(fib\)

对于每个\(n\),将\(fib\)从大到小枚举,求出\(n\)的无相邻\(1\)的\(fib\) 码

那么求出每个\(1\)有多少种可能的拆法,就可以通过乘法原理求出答案

可能的拆法数依赖于相邻两个\(1\)之间的间隔

从右往左dp

dp[i][1]表示这一位不分解的方案数

dp[i][0]表示这一位分解了(留出一个0的空位) 的方案数

那么就有

\(dp[i][1]=dp[i+1][0]+dp[i+1][1]\)

\(dp[i][0]=dp[i+1][0]*\lfloor(ps[i]-ps[i+1])/2\rfloor+dp[i+1][1]*\lfloor(ps[i]-ps[i+1]-1)/2\rfloor\)

solution

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL; inline int ri(){
int x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
return f?x:-x;
} inline LL rl(){
LL x=0;bool f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(;isdigit(c);c=getchar()) x=x*10+c-48;
return f?x:-x;
} int m;
LL n;
LL fib[93];
int ps[93],tt;
int dp[93][2]; LL getans(LL n){
int i;
for(tt=0,i=90;i>0;--i) if(n>=fib[i]){
n-=fib[i];
ps[++tt]=i;
}
ps[++tt]=0; dp[tt][1]=1; dp[tt][0]=0;
for(i=tt-1;i>0;--i){
dp[i][0]= dp[i+1][1]*((ps[i]-ps[i+1]-1)/2) + dp[i+1][0]*((ps[i]-ps[i+1])/2);
dp[i][1]= dp[i+1][0] + dp[i+1][1];
} return dp[1][0]+dp[1][1];
} int main(){ int i; for(fib[0]=fib[1]=1,i=2;i<=90;i++) fib[i]=fib[i-1]+fib[i-2]; m=ri();
while(m--){
n=rl();
printf("%I64d\n",getans(n));
} return 0;
}

最新文章

  1. asp.net(C#)利用QRCode生成二维码(续)-在二维码图片中心加Logo或图像
  2. Python Charts库的使用
  3. LeetCode 2. Add Two Numbers swift
  4. ArcGIS中的style样式的使用
  5. Div 3 - SGU 105(找规律)
  6. gradle 集成到myeclipse
  7. ViewState探索
  8. java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
  9. C# 学习笔记 C#基础
  10. smarty模板自定义变量
  11. SQL优化 MySQL版 - 避免索引失效原则(二)
  12. JavaScript(三)
  13. 【NOI2019模拟】搬砖
  14. mysql date
  15. Intel支持八九代酷睿的B365芯片组将登场亮相
  16. HBase数据模型和读写原理
  17. Linux 上传代码到github
  18. hbase shell 启动报错
  19. 统计web日志里面一个时间段的get请求数量
  20. 线程、进程、daemon、GIL锁、线程锁、递归锁、信号量、计时器、事件、队列、多进程

热门文章

  1. zabbix告警时间和恢复时间相同的解决方法
  2. 六、Linux 文件基本属性
  3. linux关于权限
  4. php中 为什么验证码 必须要开启 ob_clean 才可以显示
  5. MTCNN学习资源
  6. selenium中webdriver跳转新页面后定位置新页面的两种方式
  7. 解决Uva网站打开慢的问题
  8. poj 3292 H-素数问题 扩展艾氏筛选法
  9. 笔记-python-变量作用域
  10. Diycode开源项目 NodeListFragment分析