hdu 2604 Queuing【递推+矩阵快速幂】


Queuing

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


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. 
hdu 2604 Queuing【递推+矩阵快速幂】_第1张图片
  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
 

递推过程+思路:

老老实实的写dfs程序算出前20项的值。

测试程序:

#include<stdio.h>
#include<string.h>
using namespace std;
int ans;
int n;
void dfs(char output[],int cur,int num)
{
    if(cur==num)
    {
        int ok=1;
        for(int i=0;i<n;i++)
        {
            printf("%c",output[i]);
            if(i>=2)
            {
                if(output[i]=='f'&&output[i-1]=='f'&&output[i-2]=='f')ok=0;
                if(output[i]=='f'&&output[i-1]=='m'&&output[i-2]=='f')ok=0;
            }
        }
        if(ok==1)ans++;
        if(ok==0)printf("no");
        printf("\n");
        return ;
    }
    for(int i=0;i<2;i++)
    {
        if(i==0)output[cur]='f';
        else output[cur]='m';
        dfs(output,cur+1,num);
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        ans=0;
        char output[100];
        memset(output,'\0',sizeof(output));
        output[0]='f';
        dfs(output,1,n);
        output[0]='m';
        dfs(output,1,n);
        printf("%d\n",ans);
    }
}
然后写出各项值,慢慢慢慢找规律,推出Fn=Fn-1+Fn-3+Fn-4

然后直接写程序,实力TLE,转战矩阵快速幂,通过矩阵乘法:

? ? ? ? ) X ( Fn ) = ( Fn+1 )
( ? ? ? ? )   ( Fn-1 )   ( Fn )
( ? ? ? ? )   ( Fn-2 )   ( Fn-1 )
( ? ? ? ? )   ( Fn-3 )   ( Fn-2 )
推出矩阵:

1 0 1 1

1 0 0 0

0 1 0 0

0 0 1 0

然后慢慢幂就行了~记住该mod的地方一定要mod

AC代码:

 #include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int n,mod;
typedef struct Matrix
{
    int mat[4][4];
}matrix;
matrix A,B;
int dir[4]={9,6,4,2};
int output[4]={2,4,6,9};
Matrix matrix_mul(matrix a,matrix b)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    int i,j,k;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<4;k++)
            {
                c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                c.mat[i][j]%=mod;
            }
        }
    }
    return c;
}
Matrix matrix_quick_power(matrix a,int k)//矩阵快速幂0.0
{
    matrix b;
    memset(b.mat,0,sizeof(b.mat));
    for(int i=0;i<4;i++)
    b.mat[i][i]=1;//单位矩阵b
    while(k)
    {
        if(k%2==1)
        {
            b=matrix_mul(a,b);
            k-=1;
        }
        else
        {
            a=matrix_mul(a,a);
            k/=2;
        }
    }
    return b;
}
int main()
{
    while(cin>>n>>mod)
    {
        if(n==0)
        {
            printf("0\n");
            continue;
        }
        A.mat[0][0]=1;A.mat[0][1]=0;A.mat[0][2]=1;A.mat[0][3]=1;//我们通过推论得到的矩阵A
        A.mat[1][0]=1;A.mat[1][1]=0;A.mat[1][2]=0;A.mat[1][3]=0;
        A.mat[2][0]=0;A.mat[2][1]=1;A.mat[2][2]=0;A.mat[2][3]=0;
        A.mat[3][0]=0;A.mat[3][1]=0;A.mat[3][2]=1;A.mat[3][3]=0;
        if(n>=4)
        B=matrix_quick_power(A,n-4);
        else
        {
            printf("%d\n",output[n-1]%mod);
            continue;
        }
        int ans=0;
        for(int i=0;i<4;i++)
        {
            ans+=(B.mat[0][i]*dir[i]%mod);
        }
        printf("%d\n",ans%mod);
    }
}















你可能感兴趣的:(HDU,2604)