HDU3519Lucky Coins Sequence(DP+矩陣加速)
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=3519
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
若f(n)的後兩位相同 則f(n)=f(n-2); 即在f(n-2)後面加上00或者11;
若f(n-1)的後兩位不同 則f(n)=f(n-1) 即在f(n-1)後面加上0或者1;
因此可得f(n)=f(n-1)+f(n-2);
設g(n) 為符合的個數 則g(n)=2^n-f(n); 化簡成關於g(n)的公式為 g(n)=g(n-1)+g(n-2)+2^(n-2);
程式碼如下:
#include <iostream>
#include <cstring>
using namespace std;
const int mod = 10007;
int n;
struct matrax
{
int m[4][4];
};
matrax A={
1,1,0,2,
1,0,0,0,
0,1,0,0,
0,0,0,2
};
matrax E;
void init()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
E.m[i][j]=(i==j);
}
matrax multi(matrax a,matrax b)
{
matrax c;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
c.m[i][j]=0;
for(int k=0;k<4;k++)
c.m[i][j]+=a.m[i][k]*b.m[k][j]%mod;
c.m[i][j]%=mod;
}
}
return c;
}
matrax power(matrax A,int k)
{
matrax ans=E,p=A;
while(k){
if(k&1){
ans=multi(ans,p);
k--;
}
k>>=1;
p=multi(p,p);
}
return ans;
}
int main()
{
init();
int a[4]={8,2,6,16};
while(cin>>n){
if(n<=2){
cout<<0<<endl;
continue;
}
if(n<=5){
cout<<a[n-2]<<endl;
continue;
}
matrax ans=power(A,n-5);
int x=0;
for(int i=0;i<4;i++)
x+=(ans.m[0][i]*a[4-i-1])%mod;
cout<<x%mod<<endl;
}
return 0;
}
相關文章
- 【Ac自動機+矩陣加速】poj 2778 DNA Sequence矩陣
- POJ 3744 概率dp+矩陣矩陣
- cuda 加速矩陣乘法矩陣
- HDU 1005 Number Sequence(矩陣)矩陣
- HDU 1005 Number Sequence(矩陣快速冪)矩陣
- HDU 1005 Number Sequence:矩陣快速冪矩陣
- POJ 2778-DNA Sequence(AC自動機+構建鄰接矩陣+矩陣快速冪)矩陣
- HDU 4686 Arc of Dream(矩陣加速遞推)矩陣
- P1939 【模板】矩陣加速(數列)矩陣
- 矩陣經典題目七:Warcraft III 守望者的煩惱(矩陣加速遞推)矩陣Raft
- poj--2778DNA Sequence+AC自動機+矩陣快速冪矩陣
- 生成螺旋矩陣(方陣、矩陣)矩陣
- 鄰接矩陣、度矩陣矩陣
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 3D圖形:矩陣的行列式,矩陣的逆、正交矩陣、齊次矩陣3D矩陣
- 矩陣中最大的二維矩陣矩陣
- 讀取大型稀疏矩陣&KNN演算法的OpenCL加速版本矩陣KNN演算法
- 機器學習中的矩陣向量求導(五) 矩陣對矩陣的求導機器學習矩陣求導
- 矩陣和陣列矩陣陣列
- 理解矩陣矩陣
- 矩陣相乘矩陣
- 矩陣分解矩陣
- 稀疏矩陣矩陣
- Numpy 矩陣矩陣
- 穿越矩陣矩陣
- 混淆矩陣矩陣
- 螺旋矩陣矩陣
- 魔方矩陣矩陣
- 矩陣乘法矩陣
- 快手矩陣管理平臺,矩陣管理有方法矩陣
- Wannafly模擬賽 矩陣 二維矩陣hash矩陣
- HDU3415 Max Sum of Max-K-sub-sequence (DP+單調佇列)佇列
- 矩陣:如何使用矩陣操作進行 PageRank 計算?矩陣
- HDU4427Math Magic (dp+滾動陣列)陣列
- 視訊矩陣矩陣
- 高斯消除矩陣矩陣
- 矩陣轉置矩陣