HDU 2604_递推+矩阵快速幂
来源:互联网 发布:java聊天室界面代码 编辑:程序博客网 时间:2024/05/22 15:31
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5546 Accepted Submission(s): 2414
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 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.
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.
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 84 74 8
Sample Output
621
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("%lld\n", a.v[4 - l][0] % MOD); } else { c =mul(b,(l-4)); c = c * a; printf("%lld\n", c.v[0][0] % MOD); } } return 0; }
0 0
- HDU 2604_递推+矩阵快速幂
- 【递推&矩阵快速幂】hdu 2604
- HDU 2604 递推 + 矩阵快速幂
- hdu 2842(矩阵快速幂+递推)
- hdu 3519 递推+矩阵快速幂
- HDU 2604-Queuing(递推+矩阵快速幂)
- HDU 2604 Queuing (递推+矩阵快速幂)
- hdu 2604 Queuing 递推+矩阵快速幂
- hdu 2604 Queuing【递推+矩阵快速幂】
- HDU 2604 Queuing [递推]【矩阵快速幂】
- HDU-2604(递推+矩阵快速幂)
- HDU 2604 Queuing (递推+ 矩阵快速幂)
- hdu 4549(矩阵优化递推,数论,二分快速幂)
- HDU 4565 So Easy! 递推数列+矩阵快速幂
- hdu 4565 So Easy! 递推+矩阵快速幂
- HDU 2842 Chinese Rings(矩阵快速幂+递推)
- hdu 5015 233 Matrix (递推 矩阵快速幂)
- hdu 2855 Fibonacci Check-up【递推+矩阵快速幂】
- 黄聪:Python+NLTK自然语言处理学习(一):环境搭建
- 【怎样写代码】参数化类型 -- 泛型(二):泛型的优点
- 05 信号与槽
- 回溯算法
- 业界难题-“跨库分页”的四种方案
- HDU 2604_递推+矩阵快速幂
- 第八届山东省赛 C sdut 3895 fireworks(组合数+逆元)
- PAT 1004 Counting Leaves(BFS+模拟树)
- 欢迎使用CSDN-markdown编辑器
- 对Spring的 IOC DI AOP的理解
- 简述Hadoop和Spark的异同
- 网络编程之Select模式
- The Eclipse executable launcher was unable to locate its companion shared library
- MFS分布式文件系统