hdu 3519 Lucky Coins Sequence

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
 

矩阵快速幂 + 二分

题意:幸运硬币序列就是存在两个以上相邻的硬币正反面一样,那么就罗列仅有两个相邻的情况,最后用2^n减去那个值
仅有两个相邻的状态相同的情况,如果从开始只有一个硬币开始,存在正反两种情况,然后就跟菲波那契数列一样了,现在我们就是要加硬币,可以加一枚硬币,也可以是加两枚(这个就是,正的时候加正反,反的时候加上反正,这样就恰好达到相邻只有两枚状态一样的结果了)所以这个就是菲波那契项。。。。最后的答案就是 2^n-Fibonacci(n+1)  这两部分用二分求,和快速幂求斐波那契项。
WA了好几次,原因是求2^n的二分里面,没有对2的幂取模,忘写了。。。wa的心疼。。

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值