您的位置:首页 > 产品设计 > UI/UE

HDU HDU 2604 Queuing 矩阵乘法

2016-11-01 18:32 183 查看

                                                         Queuing

[b]                                                           Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

                                                                                    Total Submission(s): 4879    Accepted Submission(s): 2161
[/b]

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
Input a length L (0 <= L <= 10 6) and M.

[align=left]Output[/align]
Output K mod M(1 <= M <= 30) where K is the number of E-queues with length L.

[align=left]Sample Input[/align]

3 8
4 7
4 8

[align=left]Sample Output[/align]

6
2
1

[align=left]Author[/align]
WhereIsHeroFrom
 

[align=left]Source[/align]
HDU 1st “Vegetable-Birds Cup” Programming Open Contest

题意: 给你 L的长度的字符(只包含mf)  求出 E-queues mod M
可以推出 f(n)=f(n-1)+f(n-3)+f(n-4);
所以我们维护  f(n)  f(n-1)f(n-2) f (n-3 ) f(n-4) 的值
AC code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llp 4
ll mod,n;
struct matrix
{
ll arr[llp+1][llp+1];
void clear()
{
memset(arr,0,sizeof(arr));
}
matrix operator *(const matrix &B) const
{
matrix C;
C.clear();
for(int i=1; i<=llp; i++)
{
for(int j=1; j<=llp; j++)
{
for(int k=1; k<=llp; k++)
{
C.arr[i][j]=(C.arr[i][j]+(arr[i][k]*B.arr[k][j])%mod)%mod;
}
}
}
return C;
}
};
matrix matrixPow(ll n,matrix B)
{
matrix A;A.clear();
for(int i=1; i<=llp; i++)
{
A.arr[i][i]=1;
}
while(n)
{
if(n&1)
A=A*B;
B=B*B;
n>>=1;
}
return A;
}
int main()
{
while(scanf("%lld%lld",&n,&mod)!=EOF)
{
matrix x,y;x.clear();y.clear();
x.arr[1][1]=x.arr[1][3]=x.arr[1][4]=x.arr[2][1]=x.arr[3][2]=x.arr[4][3]=1;
y.arr[1][1]=9;y.arr[2][1]=6;y.arr[3][1]=4;y.arr[4][1]=2;
if(n > 4)
{
n=n-4;
matrix res = matrixPow(n,x);
matrix ans = res*y;
printf("%lld\n",ans.arr[1][1]);
}
else if(n == 0)
printf("0\n");
else
{
printf("%lld\n",y.arr[4-n+1][1]%mod);
}
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: