HDU2064 Queuing 矩阵快速幂

Queuing

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4872    Accepted Submission(s): 2158


Problem Description
Queues and Priority Queues are data structures which are known to most computer scientists. The Queue occurs often in our daily life. There are many people lined up at the lunch time. 

  Now we define that ‘f’ is short for female and ‘m’ is short for male. If the queue’s length is L, then there are 2 L numbers of queues. For example, if L = 2, then they are ff, mm, fm, mf . If there exists a subqueue as fmf or fff, we call it O-queue else it is a E-queue.
Your task is to calculate the number of E-queues mod M with length L by writing a program.
 

Input
Input a length L (0 <= L <= 10  6) and M.
 

Output
Output K mod M(1 <= M <= 30) where K is the number of E-queues with length L.
 

Sample Input
  
  
3 8 4 7 4 8
 

Sample Output
  
  
6 2 1
 

Author
WhereIsHeroFrom
 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604

代码如下:
<span style="font-size:24px;"></span><pre name="code" class="cpp">#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int mod;
struct My
{
    long long cun[4][4];
    My operator*(const My m)const
    {
        My temp;
        for(int i=0; i<4; i++)
        {
            for(int j=0; j<4; j++)
            {
                temp.cun[i][j]=0;
                for(int k=0; k<4; k++)
                {
                    temp.cun[i][j]+=cun[i][k]*m.cun[k][j]%mod;
                    temp.cun[i][j]%=mod;
                }
            }
        }
        return temp;
    }
};

My my,lin;
int as[4][4]={
1,1,0,0,
0,0,1,0,
1,0,0,1,
1,0,0,0
};
long long Pow(int k){
while(k)
{
    if(k&1)my=my*lin;
    k>>=1;
    lin=lin*lin;
}
return my.cun[0][0]%mod;
}
void init(){
memset(my.cun,0,sizeof(my.cun));
my.cun[0][0]=9;my.cun[0][1]=6;my.cun[0][2]=4;my.cun[0][3]=2;
for(int  i=0;i<4;i++)
    for(int j=0;j<4;j++)
    lin.cun[i][j]=as[i][j];
}

int main()
{
int aa[]={0,2,4,6,9};
int k;
while(cin>>k>>mod)
{
    init();

    if(k<5){cout<<aa[k]%mod<<endl;continue;}
long long ac=Pow(k-4);
cout<<ac<<endl;
}
    return 0;
}

 
      这道题是一道比较典型的矩阵快速幂,首先看到题的第一感觉就是要用快速幂,因为数据量实在是太大了,只有logn的算法的才可以。。。。于是就想到了快速幂,然而这里有一个问题就是,怎样求递推式,当然可以多算出几个如  
 F[0] = 0  
 , F[1] = 2 , F[2] = 4 , F[3] = 6 , F[4] = 9 , F[5] = 15;这样也就可以看出规律:F[n] = F[n-1]+F[n-3]+F[n-4],当N大于5的时候成立。 
  
 
    当然也可以根据他给出的条件退=推出来,现在讨论有N和个字符的时候,N>=5,这时候如果第n个字符是M的话,前面的N-1个字符可以随意,就有了F[N-1]种情况,如果第N个字符是F的话,就要考虑前面的第N-1个字符和第N-2个字符,很明显他们不可以是FM,FF;即只有可能是MF,MM;但他们为MM的时候前面的N-3个字符可以随意,就有F[N-3]种,当为MF的时候还要考虑一下第N-3个字符,因为他有可能和MF形成FMF,所以·当这两个字符为MF时,第N-3个字符只有可能是M,前面的N-4个字符随意,就有F[N-4];综上所述,F[n] = F[n-1]+F[n-3]+F[n-4]。。。。












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值