ACM--dyx--steps--3.1.8--Queuing

Queuing

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1131 Accepted Submission(s): 471
 
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
 
Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest
 
Recommend
lcy

#include<iostream>
#include<cstring>
using namespace std;
struct dyx
{
    int wyx[5][5];
};
//矩阵快速幂,构造系数矩阵
dyx e;//单位矩阵./
dyx coe_mat;//系数矩阵
int k;//需要取余的那个数.
int L;
int fun_mat[5];//基向量;
dyx ans;
//构建矩阵;
void con_mat()
{
    //递推公式为:f[n]=f[n-1]+f[n-3]+f[n-4];
    //根据矩阵构建,应该有以下的形式.
    /*|f[n]  |    |1 0 1 1|   |f[n-1]|     |1 0 1 1|^n-4  |f[4]|
      |f[n-1]|--->|1 0 0 0| X |f[n-2]|---- |1 0 0 0|    X |f[3]|
      |f[n-2]|--->|0 1 0 0|   |f[n-3]|---- |0 1 0 0|      |f[2]|
      |f[n-3]|    |0 0 1 0|   |f[n-4]|     |0 0 1 0|      |f[1]|
    */
    fun_mat[1]=9;
    fun_mat[2]=6;
    fun_mat[3]=4;
    fun_mat[4]=2;
    for(int i=1;i<=4;i++)
    {
        //将基数也同样对k取余;
        fun_mat[i]=fun_mat[i]%k;
    }
    for(int i=1;i<=4;i++)
    {
        //单位矩阵的构建,用于矩阵快速幂当中.
        e.wyx[i][i]=1;
    }
    //进行系数矩阵的构建,
    memset(coe_mat.wyx,0,sizeof(coe_mat.wyx));
    coe_mat.wyx[1][1]=coe_mat.wyx[1][3]=coe_mat.wyx[1][4]=1;
    coe_mat.wyx[2][1]=coe_mat.wyx[3][2]=coe_mat.wyx[4][3]=1;
}
//进行矩阵的乘法运算;
dyx mat_mult(dyx a,dyx b)
{
    dyx ans;
    memset(ans.wyx,0,sizeof(ans.wyx));
    for(int i=1;i<=4;i++)//表示矩阵的行
    for(int j=1;j<=4;j++)//表示矩阵的列
    {
        for(int m=1;m<=4;m++)//在第一个矩阵那里表示矩阵的列,表示第二个矩阵的行.
        ans.wyx[i][j]=(ans.wyx[i][j]+a.wyx[i][m]*b.wyx[m][j]);
        ans.wyx[i][j]=ans.wyx[i][j]%k;
    }
    return ans;
}
//进行矩阵的快速幂乘法.
dyx quick_pow(dyx a,int b)//矩阵a的b次幂
{
    dyx ans=e;
    while(b)
    {
        if(b&1)
        ans=mat_mult(ans,a);
        b>>=1;
        a=mat_mult(a,a);
    }
    return ans;
}
void set()
{
    int Ans=0;
    //进行最后结果的输出.
    for(int i=1;i<=4;i++)
    {
        Ans+=(ans.wyx[1][i])*fun_mat[i];
        Ans=Ans%k;
    }
    cout<<Ans<<endl;
}
int main()
{
    while(cin>>L>>k)
    {
        con_mat();
        if(L>4)
        ans=quick_pow(coe_mat,L-4);
        if(L>4)
        set();
        else
        {
            if(L==1)
            cout<<fun_mat[4]<<endl;
            else if(L==2)
            cout<<fun_mat[3]<<endl;
            else if(L==3)
            cout<<fun_mat[2]<<endl;
            else if(L==4)
            cout<<fun_mat[1]<<endl;
        }
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值