Lucky Coins Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 581 Accepted Submission(s): 301
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
Recommend
zhengfeng
#include <cstdio>
#include <algorithm>
#include <iostream>
#define mod 10007
using namespace std;
struct Matrix
{
int m[3][3];
};
Matrix E,B;
void Init()
{
E.m[1][1]=E.m[2][2]=1;
E.m[1][2]=E.m[2][1]=0;
B.m[1][1]=B.m[1][2]=1;
B.m[2][1]=1;
B.m[2][2]=0;
}
Matrix Multi(Matrix A,Matrix B)
{
Matrix ans;
for(int i=1;i<=2;i++)
for(int j=1;j<=2;j++)
{
ans.m[i][j]=0;
for(int k=1;k<=2;k++)
{
ans.m[i][j]+=A.m[i][k]*B.m[k][j];
ans.m[i][j]%=mod;
}
}
return ans;
}
Matrix Pow(Matrix A,int k)
{
Matrix ans=E;
while(k)
{
if(k&1)
{
k--;
ans=Multi(ans,A);
}
else
{
k/=2;
A=Multi(A,A);
}
}
return ans;
}
int POW(int a,int b)
{
int ans=1;
while(b)
{
if(b&1)
{
b--;
ans*=a;
ans%=mod;
}
else
{
b/=2;
a*=a;
a%=mod;
}
}
return ans;
}
int p;
void solve()
{
Matrix P=Pow(B,p);
int ans=POW(2,p);
ans-=(2*P.m[1][1])%mod;
while(ans<0)
ans+=mod;
printf("%d\n",ans%mod);
}
int main()
{
Init();
while(scanf("%d",&p)!=EOF)
{
if(p==0)
printf("0\n");
else
solve();
}
return 0;
}