Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3712 Accepted Submission(s): 1680
Total Submission(s): 3712 Accepted Submission(s): 1680
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
这题先要推公式,有两种方法:
1.记f[i]为前i个人得到的方案数,那么有f[n]=f[n-1]+f[n-2]+f[n-3]+f[n-4],然后用矩阵快速幂的时候注意要构造【f[n],f[n-1],f[n-2],f[n-3]】*A=【f(n+1),f(n),f(n-1),f(n-2)】,这里非常重要!不要构造为【f[n],f[n-1],f[n-3],f[n-4]】,因为这样不能构造,不知道f[n-2],然后得到
1 1 0 0
A= 0 0 1 0
1 0 0 1
1 0 0 0
然后就可以算了,这里先初始化30个矩阵的乘法,然后再算快了很多。
2.记m[i]为前i个数以m为结尾的合理方案数,f[i]为前i个数以f为结尾的合理方案数。
那么m[i]=f[i-1]+m[i-1]
f[i]=m[i-3]+m[i-2]
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
ll MOD;
struct matrix{
ll n,m,i;
ll data[5][5];
void init_danwei(){
for(i=0;i<n;i++){
data[i][i]=1;
}
}
}d[99];
matrix multi(matrix &a,matrix &b){
ll i,j,k;
matrix temp;
temp.n=a.n;
temp.m=b.m;
for(i=0;i<temp.n;i++){
for(j=0;j<temp.m;j++){
temp.data[i][j]=0;
}
}
for(i=0;i<a.n;i++){
for(k=0;k<a.m;k++){
if(a.data[i][k]>0){
for(j=0;j<b.m;j++){
temp.data[i][j]=(temp.data[i][j]+(a.data[i][k]*b.data[k][j])%MOD )%MOD;
}
}
}
}
return temp;
}
matrix fast_mod(matrix &a,ll n){
matrix ans;
ans.n=a.n;
ans.m=a.m;
memset(ans.data,0,sizeof(ans.data));
ans.init_danwei();
ll k=1;
while(n>0){
if(n&1)ans=multi(ans,d[k]);
k++;
n>>=1;
}
return ans;
}
void init()
{
ll i,j;
d[1].n=d[1].m=4;
memset(d[1].data,0,sizeof(d[1].data));
d[1].data[0][0]=d[1].data[0][1]=d[1].data[1][2]=d[1].data[2][0]=d[1].data[2][3]=d[1].data[3][0]=1;
for(i=2;i<25;i++){
d[i]=multi(d[i-1],d[i-1]);
}
}
int main()
{
ll n,m,i,j;
while(scanf("%lld%lld",&n,&MOD)!=EOF)
{
init();
matrix a;
a.n=a.m=4;
memset(a.data,0,sizeof(a.data));
a.data[0][0]=a.data[0][1]=a.data[1][2]=a.data[2][0]=a.data[2][3]=a.data[3][0]=1;
matrix ant;
ant=fast_mod(a,n-1);
matrix cnt;
cnt.n=1;cnt.m=4;
cnt.data[0][0]=9;
cnt.data[0][1]=6;
cnt.data[0][2]=4;
cnt.data[0][3]=2;
matrix juzhen;
juzhen=multi(cnt,ant);
printf("%lld\n",juzhen.data[0][3]);
}
return 0;
}