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

来源:互联网 发布:mac菜单栏 编辑:程序博客网 时间:2024/04/30 14:51

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 2L 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 84 74 8
 
Sample Output
621
 
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