HDU3519 斐波那契数列+矩阵幂

Lucky Coins Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 203    Accepted Submission(s): 122

Problem Description

As we all know,every coin has two sides,with one side facing up and another side facing down.Now,We consider two coins's state is same if they both facing up or down.If we have N coins and put them in a line,all of us know that it will be 2^N different ways.We call a "N coins sequence" as a Lucky Coins Sequence only if there exists more than two continuous coins's state are same.How many different Lucky Coins Sequences exist?

 

 

Input

There will be sevaral test cases.For each test case,the first line is only a positive integer n,which means n coins put in a line.Also,n not exceed 10^9.

 

 

Output

You should output the ways of lucky coins sequences exist with n coins ,but the answer will be very large,so you just output the answer module 10007.

 

 

Sample Input

3

4

 

 

Sample Output

2

6

 

 

咋一看还以为是 DP, 当看到规模的时候傻眼了 ,10^9!!!!! 这下咋办

后来才知道是要推公式的 , 问了两位大牛 , 公式是这么推的 :

长度为 n 01 串一共有 2^n 种不同的排列方法 ,

f(n) 为长度是 n 的不包含连续 3 个或以上相同的 1 0 01 ,

f(1)=2,f(2)=4,f(3)=6,f(4)=10

n>4 的时候 , 分情况考虑 :

1、   如果是以 00 或者 11 结尾 , 则分别有 f(n-2)/2 种情况 , 加起来就是 f(n-2) .

2、   如果是以 01 或者 10 结尾 , 则第 n 个字符要和第 n-1 个字符不一样 , 那么分别有 f(n-1)/2 , 加起来就是 f(n-1)

则统计起来就是 f(n)=f(n-1)+f(n-2), 题目要求的是包含连续三个相同的 0 1 串的串数 , 那就是用 a[n]=(2^n-f(n))%10007.

然而这样还不好求 , 先不看 %10007, 转换成递推公式是 a[n]=a[n-1]+a[n-2]+2^(n-2),

转换成矩阵 :

a[n]              1  1  1         a[n-1]

a[n-1]    =   1  0  0    *   a[n-2]

2^(n-1)       0  0  2         2^(n-2)

这样就可以用矩阵幂快速算出 a[n], 复杂度为 O(logn)

 

 

 

代码如下 :

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值