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. Effective C++学习笔记(Part One:Item 1-4)

    最近的最终effectvie C++仔细阅读侧,我很惊讶C++动力和魅力.最近的" LL最近记得阅读体验和读书笔记其.必要查找使用,是什么假设总结不合适.欢迎批评: 如今仅仅列出框架,近期会 ...

  2. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

  3. 网络编程easy错误点-手知道

    通常的网络编程socket编程.实际上.socket编程并不仅仅是满足网络间不同主机之间的通信,它也能实现同一台主机上不同进程间的通信需求. 其体如今创建socket时的參数的不同: int sock ...

  4. js之面向对象----封装篇

    学习了一天的面向对象总结一下,共分为三类 - -! 老规矩 第一部分是概念性知识!!! 面向对象编程,我们可以把他想象成我们在造人.一个对象便是一个人,这个人有胳膊有腿,这便是一个对象的属性或者方法. ...

  5. HTML5游戏开发引擎Pixi.js完全入门手册(二)元素对象属性解析

    下面,我们来解释下PIXI里面对象的各个属性.. 首先我们来看看这个各个元素对象里面到底长啥样.. alpha Number 整个舞台对象的透明度. buttonMode Boolean 渲染是否作为 ...

  6. Linux C/C++计划Shell命令大杂烩(1)

    1, 请参见发行信息 cat /etc/issue 2, 查看内核版本号 uname -r 查看内核版本号 uname -p 查看处理器类型32bit/64bit uname -n 查看网络主机名(o ...

  7. DevExpress中获取RichTextEdit中RichEditControl的两种方式

    方式一: var rte = sender as RichTextEdit; control = rte.Controls[] as RichEditControl; 方式二: PropertyInf ...

  8. Winform中node.Text重命名时窗口无响应假死的解决方法

    用户控件中有一个树,窗体使用了这个控件,但是重命名时执行node.text="XXXX" 执行了很长时间,大约9s,在此期间winform界面假死,尝试过多线程异步委托的方式来操作 ...

  9. 浏览器扩展系列————在WPF中定制WebBrowser快捷菜单

    原文:浏览器扩展系列----在WPF中定制WebBrowser快捷菜单 关于如何定制菜单可以参考codeproject上的这篇文章:http://www.codeproject.com/KB/book ...

  10. UITableView的常用方法

    一.UITableView的代理方法 #pragma mark 每一行的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtI ...