Lucky Coins Sequence

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

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
 
Source
分析题意,我们可以发现就是dp,怎么求递推公式呢?dp[i][j]表示,有i位长,j最后几位是相连的!

dp[i][3]=dp[i-1][2];

dp[i][2]=dp[i-1][1];

dp[i][1]=dp[i-1][1]+dp[i-1][2];

dp[1][1]=2;dp[1][2]=0;dp[1][3]=0;

这样,我们就可以转化为矩阵求和了!

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define mod 10007 struct node {
int m[4][4];
node operator *(node b) const//重载乘法
{
int i,j,k;
node c;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
c.m[i][j]=0;
for(k=0;k<4;k++)
{
c.m[i][j]+=m[i][k]*b.m[k][j];
c.m[i][j]%=mod;//都要取模
}
}
return c;
}
};
node original,result;
void quickm(int n)
{
node a,b;
b=original;a=result;
while(n)
{
if(n&1)
{
b=b*a;
}
n=n>>1;
a=a*a;
}
printf("%d\n",2*b.m[0][3]%mod);
}
int main ()
{ int i,j,n;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
original.m[i][j]=(i==j)?1:0;//初始化为单位矩阵
}
memset(result.m,0,sizeof(result.m));
result.m[0][0]=result.m[0][1]=result.m[1][0]=result.m[1][2]=result.m[2][3]=1;
result.m[3][3]=2;
while(scanf("%d",&n)!=EOF)
{ quickm(n);
}
return 0;
}

poj3519 Lucky Coins Sequence矩阵快速幂的更多相关文章

  1. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  2. HDU5950 Recursive sequence —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others)   ...

  3. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  4. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  5. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

  6. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  7. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  8. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  9. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

随机推荐

  1. Composer生成PHP依赖包

    近期在用一些扩展或者类库的时候,偶尔会碰到使用Composer生成php包的问题.总结步骤如下:window下安装composer:参考http://www.kankanews.com/ICkengi ...

  2. MySQL JDBC的queryTimeout坑

    遇到一个MySQL JDBC跑execute规定的方法queryTimeout坑,更恶心,无论是BUG,不能,^_^,为什么要说?请看下面的说明: 现象: 用同一个Connection运行大批量SQL ...

  3. 深入了解Libgdx中间Skin分类

    文不是直接翻译.. . 本文在Libgdx的官方wiki的基础上,加上一些自己的理解.所以,难免会一些甚至是非常多的理解非常片面的东西.写的不好,还请见谅.... 事实上 事实上.在LibGDX的官方 ...

  4. IE8升级新版Flash Player ActiveX14导致的discuz图片附件无法上传 解决方法

    架不住sb adobe的频繁升级提示,手欠升级到了了flash player 14,结果IE8下全部discuz论坛中都无法看到上传图片的button了 没办法,遇到问题就解决吧 刚好在解决IE11遇 ...

  5. elasticsearch的rest搜索--- 总述

    目录: 一.针对这次装B 的解释 二.下载,安装插件elasticsearch-1.7.0   三.索引的mapping 四. 查询 五.对于相关度的大牛的文档 一.针对这次装B 的解释  因为现在又 ...

  6. Msgbox消息框

    于"自"<软件开发工具>一本书有这样的制剂,让我回忆--程序员可以做,让用户做:它允许用户做的少,即使是那些谁需要做的程序设置.我们应该做的. 这不是宣言.该是践行. ...

  7. hust1384---The value of F[n]

    Description For any integer i>=3 we have F[i]=(F[i-1]+2*F[i-2]+3*F[i-3])%9901; Now give you F[0], ...

  8. 【SSH三大框架】Hibernate基础第六篇:多对一关联关系的映射、分析及加入、查询

    这里举样例用的是:部门与员工的关系. 一个部门能够相应多个员工,这就是非常明显的多对一关联关系. 我们须要建立两个实体类:员工(Employee).部门(Department) 员工类:Employe ...

  9. php中soap应用

    原文:php中soap应用 SOAP:简单对象访问协议 (SOAP:Simple Object Access Protocol) 简单对象访问协议(SOAP)是一种轻量的.简单的.基于 XML 的协议 ...

  10. 继续推荐几款VisualStudio的插件

    原文:继续推荐几款VisualStudio的插件 继前几天推荐了一款转换vs插件的插件后,借着安装VS2013之际,把我比较喜欢的几个插件继续推荐一下. C# Outline 2013 2013 C# ...