HDU 3519解题报告

13 篇文章 0 订阅

Lucky Coins Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 812    Accepted Submission(s): 423


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   |   We have carefully selected several similar problems for you:   3524  2842  3523  3521  3520 

        这道题是一道dp+矩阵快速幂的题。而在进行dp的时候,开始从正面进行求解感觉比较困难。从正面考虑,如果只是从一个长度为i的序列中有多少个连续的3个相同来写状态转移方程很不好写。而且也无法知道具体的位置,所以这样考虑不行。如果从长度为i的序列,末尾有多少个相同数字(数字只有0和1)上考虑,可以写出状态转移方程,但是也不太方便。

       但是如果我们从反面考虑就会方便很多,反面考虑一个序列中不含有连续3个数字相同。那么设dp[i]为长度为i的序列中不含有连续3个数字相同。我们考虑长度为i-1的序列,如果该序列中不含有连续3个数字相同。那么假设长度为i-1的序列的末尾数字为0,则我们不需要考虑这个末尾数字之前的一位是0还是1,直接在后边加上1即可满足长度为i的不含连续3个数字相同的序列。当然如果长度为i-1的序列末尾数字为1,那么添上0即可。还有一种情况。如果长度为i-1的序列末尾为0,我们添上0要使得它成立。那么就必须要保证长度为i-2的序列末尾数字为1。如果长度为i-2的序列末尾数字为0,那么添上11即可满足长度为i的不含连续3个数字相同的序列。如果长度为i-2的序列末尾数字为1,那么添上00即可。

       因而可得状态转移方程为dp[i]=dp[i-1]+dp[i-2]。dp[1]=2,dp[2]=4。题目所求的结果为2^n-dp[i]。当n较大时,用矩阵快速幂即可算出dp[n]的结果。

       参考代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;

const int mod=10007;
int a[2][2],ans[2][2],b[2][2],n,f[41];

ll pow_mod(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=(ans*x)%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return ans;
}

void multi(int a[2][2],int b[2][2])
{
    int t[2][2];
    rep(i,0,2)
    {
        rep(j,0,2)
        {
            t[i][j]=0;
            rep(k,0,2)
               t[i][j]=(t[i][j]+(ll)a[i][k]*b[k][j])%mod;
        }
    }
    rep(i,0,2)
    {
        rep(j,0,2)
          a[i][j]=t[i][j];
    }
}

int main()
{
   f[1]=2,f[2]=4;
   REP(i,3,40)
     f[i]=((ll)f[i-1]+f[i-2])%mod;
   while(~read(n))
   {
       if(n<=40)
       {
           printf("%d\n",((pow_mod(2,n)-f[n])%mod+mod)%mod);
           continue;
       }
       b[0][0]=4,b[1][0]=2,b[0][1]=b[1][1]=0;
       a[0][0]=a[0][1]=a[1][0]=1,a[1][1]=0;
       ans[0][0]=ans[1][1]=1,ans[0][1]=ans[1][0]=0;
       int t=n-1;
       while(t)
       {
           if(t&1) multi(ans,a);
           multi(a,a);
           t>>=1;
       }
       multi(ans,b);
       printf("%d\n",((pow_mod(2,n)-ans[1][0])%mod+mod)%mod);
   }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值