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)
代码如下 :
LuckyCoinsSequence:一种独特的硬币排列方式计数问题

本文深入探讨了LuckyCoinsSequence的数学原理,即在特定条件下不同排列方式的数量计算。通过引入公式推导,解决了一个看似简单的排列问题,但涉及的规模挑战了传统动态规划方法的极限。最终,文章揭示了一种基于矩阵快速幂的高效解决方案,展示了在大规模数据处理中应用数学与算法的重要性。
4728

被折叠的 条评论
为什么被折叠?



