概述
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5546 Accepted Submission(s): 2414
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.
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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#include <cmath>
#include <cstdlib>
typedef long long ll;
const int N = 0;
const int SIZE = 4;
int l, MOD;
struct Mat{
ll v[SIZE][SIZE]; // value of matrix
Mat() {
memset(v, 0, sizeof(v));
}
void init(ll _v) {
for(int i=0;i<4;i++){
v[i][i]=_v;
}
}
};
Mat operator * (Mat a, Mat b) {
Mat c;
for(int i=0;i<4;i++) {
for(int j=0;j<4;j++) {
c.v[i][j] = 0;
for(int k=0;k<4;k++){
c.v[i][j] += (a.v[i][k] * b.v[k][j]) % MOD;
c.v[i][j] %= MOD;
}
}
}
return c;
}
Mat operator ^ (Mat a, ll k) {
Mat c;
c.init(1);
while (k) {
if (k&1) c = a * c;
a = a * a;
k >>= 1;
}
return c;
}
Mat mul(Mat a,ll k ){
Mat tmp;
int i;
for(i=0;i<4;i++){
tmp.v[i][i]=1;
}
while(k){
if(k&1){
tmp=(tmp*a);
}
a=a*a;
k>>=1;
}
return tmp;
}
int main() {
Mat a, b, c;
// a
a.v[0][0] = 9;
a.v[1][0] = 6;
a.v[2][0] = 4;
a.v[3][0] = 2;
// b
b.v[0][0] = b.v[0][2] = b.v[0][3] = b.v[1][0] = b.v[2][1] = b.v[3][2] = 1;
//初始化地推矩阵
while (scanf("%d%d", &l, &MOD)==2) {
if (l == 0) {
puts("0");
} else if (l <= 4) {
printf("%lldn", a.v[4 - l][0] % MOD);
} else {
c =mul(b,(l-4));
c = c * a;
printf("%lldn", c.v[0][0] % MOD);
}
}
return 0;
}
最后
以上就是甜美柠檬为你收集整理的HDU 2604_递推+矩阵快速幂的全部内容,希望文章能够帮你解决HDU 2604_递推+矩阵快速幂所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复