Queuing
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5985 Accepted Submission(s): 2607
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.
![](https://i-blog.csdnimg.cn/blog_migrate/bf59a6d08eb3e06ef94e640e4278ce2a.jpeg)
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
题意: 找 不含 FFF FMF 的 子串的个数, 明显可知 一共有 2^L种; 让求 总数%M 可以手推出 F(1) = 0 F(1) =2 F(2) =4 F(3) = 6 F(4) =9 F(5) = 15 F(6) = 25
找出 关系 F(N) = F(N-1) + F (N-3) + F(N -4) F(N) = F(N-1) +F(N-2) pass掉
L 的 范围 10^6 明显一般求法 会 超longlong 或超时 , 那好办 矩阵快速幂 上场了;
构造矩阵:
代码:
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cstring>
typedef long long ll;
const int N= 12;
const int MAXN=4;
int MOD;
using namespace std;
ll num[5]={9,6,4,2,0};
struct Matrix{
ll arr[N][N];
void init()
{
memset(arr,0,sizeof(arr));
for(int i=0;i<MAXN;i++)
arr[i][i]=1;//初始化
}
void iinit()
{
memset(arr,0,sizeof(arr));
arr[0][0]=arr[0][2]=arr[0][3]=1;
for(int i=0;i<MAXN;i++)
arr[i+1][i]=1;
}
}A;
Matrix mul(Matrix X,Matrix Y)// 矩阵乘法
{
Matrix ans;
for(int i=0;i<MAXN;i++)
for(int j=0;j<MAXN;j++){
ans.arr[i][j]=0;
for(int k=0;k<MAXN;k++){
ans.arr[i][j]+=X.arr[i][k]*Y.arr[k][j];
ans.arr[i][j]%=MOD;
}
}
return ans;
}
Matrix Q_pow(Matrix B,int n)// 矩阵快速幂
{
Matrix ans;
ans.init();
while(n)
{
if(n&1)
ans=mul(ans,B);
n>>=1;
B=mul(B,B);
}
return ans;
}
Matrix Add(Matrix a,Matrix b) //(a+b)%mod 矩阵加法
{
int i,j,k;
Matrix ans;
for(i=0;i<MAXN;i++)
for(j=0;j<MAXN;j++)
{
ans.arr[i][j]=a.arr[i][j]+b.arr[i][j];
ans.arr[i][j]%=MOD;
}
return ans;
}
Matrix Sum(Matrix a,int n)// 矩阵和
{
int m;
Matrix ans,pre;
if(n==1) return ans;
m=n/2;
pre=Sum(a,m);
ans=Add(pre,mul(pre,Q_pow(a,m)));
if(n&1)
ans=Add(ans,Q_pow(a,n));
return ans;
}
int main()
{
int n;
while(~scanf("%d %d",&n,&MOD))
{
Matrix ans;
ans.iinit();
if(n<=4)
{
switch(n)
{
case 0:printf("0\n");break;
case 1:printf("%d\n",2%MOD);break;
case 2:printf("%d\n",4%MOD);break;
case 3:printf("%d\n",6%MOD);break;
case 4:printf("%d\n",9%MOD);break;
}
continue;
}
ans=Q_pow(ans,n-4);
ll res=0;
for(int i=0;i<MAXN;i++)
res+=(ans.arr[0][i]*num[i])%MOD;
printf("%lld\n",res%MOD);
}
return 0;
}