Problem I
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.
L (0 <= L <= 10 6) and
M.
M(1 <= M <= 30) where K is the number of E-queues with length
L.
#include
#include
using namespace std;
int n,mod;
struct Matrix{
int
arr[4][4];
};
Matrix unit,init;
//矩阵相乘的函数
Matrix Mul(Matrix a,Matrix b){
Matrix
c;
for(int
i=0;i<4;i++)
for(int j=0;j<4;j++){
c.arr[i][j]=0;
for(int k=0;k<4;k++)
c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j]%mod)%mod;
//cout<<c.arr[i][j]<<endl;
c.arr[i][j]%=mod;
}
return
c;
}
//进行F[n]=F[n-1]+F[n-3]+F[n-4]
Matrix Pow(Matrix a,Matrix b,int k){
while(k){
if(k&1){
b=Mul(b,a);
}
a=Mul(a,a);
//cout<<k<<endl;
k>>=1;//k/=2但是前者快点
}
return
b;
}
//初始化
void Init(){
for(int
i=0;i<4;i++)
for(int j=0;j<4;j++){
init.arr[i][j]=0;
unit.arr[i][j]=0;
}
//递推的前四项
unit.arr[0][0]=9,
unit.arr[0][1]=6,
unit.arr[0][2]=4,
unit.arr[0][3]=2;
//设置递推关系的矩阵
init.arr[0][0]=1,
init.arr[0][1]=1,
init.arr[1][2]=1,
init.arr[2][0]=1,
init.arr[2][3]=1,
init.arr[3][0]=1;
}
int main(){
//freopen("in.txt","r",stdin);
Init();
while(~scanf("%d%d",&n,&mod)){
if(n<=4){
if(n==0)
printf("0");
else if(n==1)
printf("%d\n",2%mod);
else if(n==2)
printf("%d\n",4%mod);
else if(n==3)
printf("%d\n",6%mod);
else if(n==4)
printf("%d\n",9%mod);
continue;
}
Matrix res=Pow(init,unit,n-4);
printf("%d\n",res.arr[0][0]%mod);
}
return
0;
}
Problem I的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- [js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)
上文,写完弧度与贝塞尔曲线[js高手之路] html5 canvas系列教程 - arcTo(弧度与二次,三次贝塞尔曲线以及在线工具),本文主要是关于线条的样式设置 lineWidth: 设置线条的宽 ...
- noip的一些模板(参考了神牛的博客)
一.图论 1.单源最短路 洛谷P3371 (1)spfa 已加SLF优化 419ms #include <iostream> #include <cstdio> #includ ...
- JSP入门 el表达式
我们已经知道el是jsp-2.0规范的一部分,tomcat-5.x版本以上都已经能够支持jsp-2.0规范,但在更低版本的tomcat和webphere,weblogic中还是无法使用这一便捷方式. ...
- 51nod 1270 数组的最大代价 思路:简单动态规划
这题是看起来很复杂,但是换个思路就简单了的题目. 首先每个点要么取b[i],要么取1,因为取中间值毫无意义,不能增加最大代价S. 用一个二维数组做动态规划就很简单了. dp[i][0]表示第i个点取1 ...
- httpd配置文件规则说明和一些基本指令
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- mysql客户端(Navicat)远程登录操作遇到问题1142
遇到此问题的原因是:用户user对数据库test 无权限操作. 解决方法:mysql> grant all privileges on test.* to user@'localhost' id ...
- python web框架之Tornado
说Tornado之前分享几个前端不错的网站: -- Bootstrap http://www.bootcss.com/ -- Font Awesome http://fontawesome.io/ - ...
- hadoop各个名词的理解
Hadoop家族的各个成员 hadoop这个词已经流行好多年了,一提到大数据就会想到hadoop,那么hadoop的作用是什么呢? 官方定义:hadoop是一个开发和运行处理大规模数据的软件平台.核心 ...
- WPF 中模拟键盘和鼠标操作
转载:http://www.cnblogs.com/sixty/archive/2009/08/09/1542210.html 更多经典文章:http://www.qqpjzb.cn/65015.ht ...
- EsRejectedExecutionException排错与线程池类型
1.EsRejectedExecutionException异常示例 java.util.concurrent.ExecutionException: RemoteTransportException ...