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. jvm对大对象分配内存的特殊处理(转)

    前段日子在和leader交流技术的时候,偶然听到jvm在分配内存空间给大对象时,如果young区空间不足会直接在old区切一块过去.对于这个结论很好奇,也比较怀疑,所以就上网搜了下,发现还真有这么回事 ...

  2. NSString 筛选和最后一个空白、空行,多换行成一个新行

    - (NSString *)filterBlankAndBlankLines:(NSString *)str { NSMutableString *Mstr = [NSMutableString st ...

  3. android 之 下载管理器 无论监测在当地的设计思路

    我相信你.让应用市场.或其它下载,想不管是什么地方监测进展情况.而且很好的实现. 这里分享一个相对简单的,并防止内存溢出等..我们用一个引用弱结合View进展更新方法. Map<String, ...

  4. Access Toke调用受保护的API

    ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API 在前一篇博文中,我们使用OAuth的Client Credential Grant授权方式,在服务端 ...

  5. VC各种方法获得的窗口句柄

    AfxGetMainWnd AfxGetMainWnd获取窗口句柄本身 HWND hWnd = AfxGetMainWnd()->m_hWnd; GetTopWindow 功能:子窗体z序(Z序 ...

  6. java_代码注释风格

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><templa ...

  7. ArcGIS API for Silverlight 使用GP服务实现要素裁剪功能

    原文:ArcGIS API for Silverlight 使用GP服务实现要素裁剪功能 昨天一QQ好友问了一个关于裁剪的问题,感觉自己也没有帮上什么忙,之后自己做了一个裁剪的例子,不过在做这个例子的 ...

  8. POJ 2109 :Power of Cryptography

    Power of Cryptography Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18258   Accepted: ...

  9. 选择29部分有用jQuery应用程序插件(免费点数下载)

    免积分下载:http://download.csdn.net/detail/yangwei19680827/7238711 原文地址:http://www.cnblogs.com/sxwgf/p/36 ...

  10. 通讯录C++console application

    #include<iostream> #include<fstream> #include<string> #include<cstring> #inc ...