cf 126D - Fibonacci Decomposition
题目大意
\(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;
}
cf 126D - Fibonacci Decomposition的更多相关文章
- Codeforces 126D Fibonacci Sums 求n由随意的Sum(fib)的方法数 dp
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/34120269 题目链接:点击打开链接 题意 ...
- CF(D. Fibonacci Sums)dp计数
题目链接:http://codeforces.com/contest/126/problem/D 题意:一个数能够有多种由互不同样的斐波那契数组成的情况: 解法:dp,easy证明:每一个数通过贪心能 ...
- CF 981C Useful Decomposition
题面 题目大意 给定一棵树,要求划分出几条链,使这几条链交于一点. 解题思路 因为所有链都要交于一点,所以必须交于一个度数最多的点.这样就形成了一个菊花形.然后从这个点出发到它的子树,判断子树的度数是 ...
- CF#213DIV2:B The Fibonacci Segment
You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for ...
- CF 914 G Sum the Fibonacci —— 子集卷积,FWT
题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...
- cf B. The Fibonacci Segment
http://codeforces.com/contest/365/problem/B #include <cstdio> #include <cstring> #includ ...
- CF 914G Sum the Fibonacci——子集卷积
题目:http://codeforces.com/contest/914/problem/G 第一个括号可以子集卷积:第三个括号可以用 FWT 异或卷积:这样算出选两个数组成 x 的方案数:三个部分的 ...
- 【CF 718C】fibonacci
题意 给你一个长度为 \(n\) 的序列 \(a\),有 \(m\) 次操作,操作分两种 \(\text{1}\space \text{l}\space \text{r}\space \text{x} ...
- Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)
第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...
随机推荐
- RPC框架 - thrift 客户端
-------客户端程序 ------ 下载 下载 thrift 源代码包 下载 thrift 的bin包 准备描述文件(使用源代码包的示例文件) \thrift-0.10.0\tu ...
- php扩展开发-变量
我们在php中用到的变量,在底层的C语言代码里是一个结构体,由四个成员组成typedef struct _zval_struct { zvalue_value value; /* 变量的值,也是一个结 ...
- JZOJ 5838. 旅游路线 最大子段和
5838. 旅游路线 Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits Goto ProblemSet Descrip ...
- Django ORM (四) annotate,F,Q 查询
annotate 可以通过计算查询结果中每一个对象所关联的对象集合,从而得出总计值(也可以是平均值或总和),即为查询集的每一项生成聚合. from django.shortcuts import re ...
- linux-最最最最最常用的操作
去除两个文件中相同的内容 比如我想把file1中不含文件file2的内容保留下来:(这个在抠一些内容的时候挺好用的) awk '{print $0}' file1 file2 |sort|uniq - ...
- Median of Two Sorted Arrays LeetCode Java
两排序好的数组,找中位数 描述There are two sorted arrays A and B of size m and n respectively. Find the median of ...
- java并发面试题-基础
多线程 java中有几种方法可以实现一个线程? 1.直接继承thread类:2.实现runnable接口: 如何停止一个正在运行的线程?可以使用正在运行的线程,支持线程中断,通常是定义一个volati ...
- java枚举类型转换为Struts2的select的数据
枚举类:AppSortEnum.java public enum AppSortEnum { CORE(0, "核心应用"), ENJOYMENT(1, "娱乐应用&qu ...
- wget常用下载命令
wget wget是一个从网络上自动下载文件的自由工具,支持通过HTTP.HTTPS.FTP三个最常见的TCP/IP协议下载,并可以使用HTTP代理.wget名称的由来是“World Wide Web ...
- 1497: [NOI2006]最大获利(最大权闭合子图)
1497: [NOI2006]最大获利 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5503 Solved: 2673 Description 新的技 ...