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. mariadb 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    [root@localhost /]# systemctl stop mariadb.service[root@localhost /]# mysqld_safe --user=mysql --ski ...

  2. .net Framework各个版本之间的发展

    原文:.net Framework各个版本之间的发展 上个星期看到了.NET 4.0框架退休日期逐渐临近文章,发现自己一直在使用NET  FrameWork,身为一个NET程序员,里面大概的区别自己还 ...

  3. HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)

    HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意:  求第n个斐波那契数的 ...

  4. POJ 2502 Subway (Dijkstra 最短+建设规划)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6689   Accepted: 2176 Descriptio ...

  5. crawler_httpurlconnection_自动编码识别

    核心思想: 1:从响应头中读取 [命中解流准确率最高] 2:如果响应头中没有,打开流从源码中读取,[取舍,如果有一般在前30行会有,前100行中寻找] 3:如果还没有,根据字节码code位置,字符识别 ...

  6. 文档流 css中间float clear和布局

    文档流 先说说什么是公文流转  什么流 它是一系列连续的东西 <div style="background-color:pink;width:40px;height:80px;&quo ...

  7. linux 编译java并打包

    一.首先是编译简单java文件(不引用外部jar包)如test.java public class test(){ System.out.println("hello world!" ...

  8. 使用jquery实现放大镜效果

    原文:使用jquery实现放大镜效果 实现原理 首先,我们讲解一下放大镜效果的实现方式: 方法一:准备一张高像素的大图,当鼠标放到原图上,加载显示大图的对应位置. 方法二:对原图片进行放大,也就是调整 ...

  9. C#实现Web文件上传的两种方法

    1. C#实现Web文件的上传 在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛. 那么使用C#如何实现文件上传的功能呢?下面笔 ...

  10. TRS_WCM(拓尔思信息技术有限公司)内容协作平台平台置标经验攻略

    TRS_WCM置标过程中经验积累 版本V4.0-2014.6.24-穿越者7号 目录 1.嵌套模板置标 1 2.栏目名称超链接置标 1 3.列表循环输出文档标题包含超链接 1 4.取既定栏目下第一篇文 ...