HDU 2604Queuing(矩阵快速幂)

来源:互联网 发布:暴风魔镜app软件 编辑:程序博客网 时间:2024/05/20 13:08

如有错误,欢迎大神指出!!!

Queuing

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


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   |   We have carefully selected several similar problems for you:  1588 1757 2606 2276 2603 



题意:n个人进行男女排队,如果是fmf,或者fff就 不符合条件,计算有多少符合条件的序列并且取余M。

题解:可以用递推或者矩阵。

首先,假设n个人符合条件的序列为f(n),若最后一个人是女性m,那么f(n)=f(n-1),若是男性f则进一步讨论;

看最后三个人(n-3) 有mmf,mff,fmf,fff,四种可能,其中fmf和fff不符合。

若是mmf那么f(n)=f(n-3),若是mff则需要在看前一位只有mmff才符合所以f(n)=f(n-4);

将所有的可能加起来得到 f(n)=f(n-1)+f(n-3)+f(n-4);

构造矩阵

pic

剩下的看代码……

ac code:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <cmath>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>#define si1(a) scanf("%d",&a)#define si2(a,b) scanf("%d%d",&a,&b)#define sd1(a) scanf("%lf",&a)#define sd2(a,b) scanf("%lf%lf",&a,&b)#define ss1(s)  scanf("%s",s)#define pi1(a)    printf("%d\n",a)#define pi2(a,b)  printf("%d %d\n",a,b)#define mset(a,b)   memset(a,b,sizeof(a))#define forb(i,a,b)   for(int i=a;i<b;i++)#define ford(i,a,b)   for(int i=a;i<=b;i++)#define LL long long#define eps 1e-8#define INF 0x3f3f3f3f#define mod 1000000007#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1using namespace std;int num[5][2];struct Matrix{    int m[4][4];}E,A;void init(){    mset(E.m,0);    for(int i=0;i<4;i++)        E.m[i][i]=1;    mset(A.m,0);    A.m[0][0]=A.m[1][0]=A.m[2][1]=A.m[3][2]=A.m[0][2]=A.m[0][3]=1;    num[0][0]=9;num[1][0]=6;num[2][0]=4;num[3][0]=2;    return ;}Matrix Mul(Matrix a,Matrix b,int M){    Matrix ans;    for(int i=0;i<4;i++)    {        for(int j=0;j<4;j++)        {            ans.m[i][j]=0;            for(int k=0;k<4;k++)            {                ans.m[i][j]=(ans.m[i][j]+a.m[i][k]*b.m[k][j])%M;            }        }    }    return ans;}Matrix quick(Matrix a,int k,int M){    Matrix t=a,d=E;    while(k)    {        if(k&1)            d=Mul(d,t,M);        k>>=1;        t=Mul(t,t,M);    }    return d;}int main(){    int n,M;    init();    while(~si2(n,M))    {        if(n<=4)        {            printf("%d\n",(num[4-n][0])%M);            continue;        }        Matrix ans=quick(A,n-4,M);        int sum=0;        for(int i=0;i<4;i++)        {            sum+=(ans.m[0][i]*num[i][0])%M;        }        printf("%d\n",sum%M);//这里需要进一步取余,错了两发才发现……
    }    return 0;}


0 0
原创粉丝点击